Git Product home page Git Product logo

Comments (6)

FaizBShah avatar FaizBShah commented on June 4, 2024 1

@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.

FaizBShah avatar FaizBShah commented on June 4, 2024 1

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.

FaizBShah avatar FaizBShah commented on June 4, 2024 1

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.

FaizBShah avatar FaizBShah commented on June 4, 2024 1

@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.

Saul9201 avatar Saul9201 commented on June 4, 2024

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.

FaizBShah avatar FaizBShah commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.