-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Unable to populate subdocument's virtual array with dynamic ref #8277
Comments
I'm having the same issue. The execution throws
On
|
On Query.prototype._completeOne = function(doc, res, callback) {
if (!doc && !this.options.rawResult) {
return callback(null, null);
}
const model = this.model;
const projection = utils.clone(this._fields);
const userProvidedFields = this._userProvidedFields || {};
// `populate`, `lean`
const mongooseOptions = this._mongooseOptions;
// `rawResult`
const options = this.options;
if (options.explain) {
return callback(null, doc);
}
if (!mongooseOptions.populate) {
return mongooseOptions.lean ?
_completeOneLean(doc, res, options, callback) :
completeOne(model, doc, res, options, projection, userProvidedFields,
null, callback);
}
console.log(this._mongooseOptions);
const pop = helpers.preparePopulationOptionsMQ(this, this._mongooseOptions);
console.log(this._mongooseOptions);
console.log(pop);
model.populate(doc, pop, (err, doc) => {
if (err) {
return callback(err);
}
console.log(this._mongooseOptions);
console.log(pop);
return mongooseOptions.lean ?
_completeOneLean(doc, res, options, callback) :
completeOne(model, doc, res, options, projection, userProvidedFields,
pop, callback);
});
}; gives the following data on a successful run:
and the following on a failed one:
I'm not able to understand how the last two ones have different values on the successful one when being used inside the callback. |
On
I'm pretty sure the bug can be isolated to |
The issue seems to be here:
Given the following data: normalizeRefPath(
"emiterType",
{
_id: "5db2fb1193e1ed2878b97291",
activeAgents: [],
isActive: true,
hasAgent: false,
creationTimeStamp: "2019-10-25T13:29:41.344Z",
clientRef: "5daf1403d5412851d02be2dc",
supportChat: [
{
_id: "5db2fb1193e1ed2878b97290",
data: 'Teste',
emiterId: "5daf1403d5412851d02be2dc",
emiterType: 'Client',
originalMessage: "5db2fb1193e1ed2878b9728e'
}
],
__v: 1
},
"supportChat.emiterId"
); it should've returned |
Oh I see now. When there are nested schemes and the child scheme has a A temporary workaround is to set the full path on child's emiterId: { type: Schema.Types.ObjectId, refPath: 'supportChat.emiterType', required: true } This issue should be fixed as soon as possible since the child document doesn't always knows how it is contained inside its parent component. |
@yeegr yes you should be able to do this: MemberSchema.virtual('user', {
ref: (doc) => doc.userRef,
localField: 'userId',
foreignField: '_id',
justOne: true
}) @VictorGaiva glad you found your issue. You could also just do |
Can I populate a dynamically referenced (with "refPath") virtual field in a subdocument array in Mongoose?
The data structure goes like
Code: models/schema
Code: controller
I will get a populated user if I add {model: 'Admin'} to the populate options, but that completely defeats the purpose.
The text was updated successfully, but these errors were encountered: