Comments (6)
@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
from mongoose.
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.
from mongoose.
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.
from mongoose.
@Saul9201 Have added a PR which prevents the applyGetters()
function from getting called if schemaType is nested
- #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.
from mongoose.
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.
from mongoose.
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
from mongoose.
Related Issues (20)
- Improve Documentation HOT 1
- BulkSave on entities with virtual properties *and* a transaction: massive performance hit and OOM crashes HOT 5
- Filter query `Condition` type might as well be `any` HOT 3
- pre middleware .save conflict with strict mode HOT 4
- Date coercion and typechecking inconsistent on `bulkWrite` HOT 3
- Bug while creating document HOT 4
- `validateAllPaths` option to `validate()` and `save()` that validates all paths in the schema, not just modified paths HOT 10
- Nested arrays using `of` result in the incorrect schema type HOT 4
- Why is updateOne invalid if it does not call the then method HOT 2
- Populated documents are flattened to ObjectIds when assigned to another document HOT 5
- Automatically recreate ChangeStream on 'close' event
- Can not update sub schema to empty object HOT 4
- Add a nullable field, its not same as "required" HOT 1
- Turn off "don't cast update pipelines by default" in favor of a more secure approach HOT 2
- Add TypeScript interface for the new PipelineStage - Vector Search HOT 1
- Add `pre` and `post` function types on Query class
- Best method to set Query and Lean defaults HOT 4
- Issue with 2dsphere indexes in nested schemas vs subdocuments HOT 2
- (Typescript) toObject on populated documents loses the structure
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.