Closed Saul9201 closed 5 months ago
@Saul9201 I've tried debugging this, and what I have found is that the reason the error is coming is because the schema of the path extras.config.paymentConfiguration
is just the string nested
. Normally, the schemas should be an object, but in this scenario, its nested. And since we are calling schema.applyGetters()
in document.js
, and a string does not have any function called applyGetters()
, that's why its failing
Found the reason from the docs. So apparently, any field which is an object but does not have the type
field in Schema is a nested schema. In your case, paymentConfiguration
field is nested schema. Now, as you can see in the highlighted para in the doc, the path for the field which is a nested schema is not actually created by Mongoose, and thus it might give error in doing population.
I think yours is a valid use case and it could be implemented by mongoose, but for now you can fix this by converting the paymentConfiguration
field into a Schema, or probably populating without using the dotted-string population method.
Personally, I feel the dotted-string approach for valid paths, even if they are inside nested docs, should work correctly.
Thank you very much @FaizBShah, I've also been debugging and reached the same conclusion. A possible workaround is to use a Subdocument instead of Nested Path, as this way the path to the extras.config.paymentConfiguration
branch is created:
const UserSchema = new mongoose.Schema({
extras: [
new mongoose.Schema({
config: new mongoose.Schema({
paymentConfiguration: new mongoose.Schema({
paymentMethods: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Code'
}
]
}),
})
})
],
});
But I agree with you, the schema with Nested Path is also a valid schema and should work correctly.
Yupp, btw just to clarify, this is only happening because your extras
field is an array. If it were a subdocument, the function would have worked perfectly fine (I've tested it in local, and its working fine). For some reason, mongoose is not able to handle nested documents within an array parent, but otherwise its able to do it. In normal case (i.e. no array parent), the schema is coming to be null
in document.js
, and its inferring the value from the document's this
instance. But in array parent case, the schema is coming to be nested
and its failing the function
@Saul9201 Have added a PR which prevents the applyGetters()
function from getting called if schemaType is nested
- https://github.com/Automattic/mongoose/pull/14443. This won't affect the result of the population of data as that has already been done by the time this function is called.
Prerequisites
Mongoose version
8.2.1
Node.js version
18.19.1
MongoDB server version
7.0.6
Typescript version (if applicable)
No response
Description
When I try to populate a document with nested paths it is not working correctly. I am getting the error: schema.applyGetters is not a function.
Steps to Reproduce
Output:
Expected Behavior
It should return the user document with the array
extras.config.paymentConfiguration.paymentMethods
populated correctly