how to populate virtual fields in the response of create document
See original GitHub issuehttps://github.com/Automattic/mongoose/issues/3225
as per this its working perfect the data from field is getting populated in virtual field in case of getById
var countrySchema = new mongoose.Schema({
capitalId: {type:String}
});
countrySchema.virtual('capital',{
ref: 'City',
localField: 'capitalId',
foreignField: '_id',
justOne: true
});
countrySchema.set('toObject', { virtuals: true });
countrySchema.set('toJSON', { virtuals: true });
Country.find().populate('capital')
======================
what about create case after create in response if i want the virtual field to get populated.
i added following code for that but its not working
countrySchema.post(“save”, async function(){ await this.populate({ path: “capital”, model: “City” }).execPopulate(); })
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
Mongoose Virtuals
In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents.
Read more >Virtual populate mongoose - Stack Overflow
You need an option virtuals: true passed into the schema creation: const tourSchema = new mongoose.Schema({ ... }, { virtuals: true }.
Read more >How to Use Virtual Fields to Populate a Field ... - Omatic Support
In the IOM profile, on the virtual field column select and set the "Function" to "Copy Field". On the same virtual field, map...
Read more >How to populate virtuals to a mongoose model using Node.js
Create a folder and add the file main.js. For populating virtual, we have to specify three necessary options:.
Read more >populate doesn't include virtuals when populate multiple ...
I have set these schema options. resourceSchema.set('toObject', { virtuals: true }); resourceSchema.set('toJSON', { virtuals: true });. User.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @indraraj26 @vkarpov15 @IslandRhythms
@NitinMagdum whatever you will do you are going to make another request in order to get the data from another collection.
As per your initial post, This will work