Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate does not consider toJSON and id:true #14620

Closed
2 tasks done
parthpatel-simbiotik opened this issue May 29, 2024 · 3 comments
Closed
2 tasks done

Populate does not consider toJSON and id:true #14620

parthpatel-simbiotik opened this issue May 29, 2024 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale

Comments

@parthpatel-simbiotik
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.0

Node.js version

21.7.1

MongoDB server version

7.0.8

Typescript version (if applicable)

No response

Description

I have 2 schema, and 1 was populating another one. and both has id:true and toJSON implemented to delete ret._id;

but not working after 8.4.0 when I switched back to 8.0.3 it is working fine. it created lot of issues since frontend was expecting id but was returning _id.

Steps to Reproduce

Try creating 2 schema and populate one from another, implement id:true and toJSON to remove any fields.

It will work on version 8.0.3 but will not work on 8.4.0

@Schema({ id: true, timestamps: true, toJSON: { virtuals: true, versionKey: false, transform(doc, ret) { delete ret._id; }, } })

PassingInfoSchema.virtual('entry', { ref: 'ParkingEntry', localField: 'entryId', foreignField: '_id', justOne: true, });

Expected Behavior

behavior of populated fields should be same as earlier.

Please look into it.

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label May 30, 2024
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels May 30, 2024
@IslandRhythms
Copy link
Collaborator

Please modify the script below to demonstrate your issue:

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String
}, { id: true, timestamps: true, toJSON: { virtuals: true, versionKey: false, transform(doc,ret) { delete ret._id ;}}})

const otherSchema = new mongoose.Schema({
  name: String,
  testId: mongoose.Schema.Types.ObjectId
});

otherSchema.virtual('entry', { ref: 'Test', localField: 'testId', foreignField: '_id', justOne: true });

const Test = mongoose.model('Test', testSchema);

const Other = mongoose.model('Other', otherSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const doc = await Test.create({
    name: 'Test Testerson'
  });

  const other = await Other.create({
    name: 'Other Otherson',
    testId: doc._id
  });
  const res = await Other.findOne().populate('entry');
  console.log('what is res', res, res.entry);
}

run();

Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Jun 14, 2024
Copy link

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale
Projects
None yet
Development

No branches or pull requests

3 participants