Skip to content
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

Nested populate doesn't work when the main property has a default: null definition #8518

Closed
Ncifra opened this issue Jan 17, 2020 · 1 comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@Ncifra
Copy link

Ncifra commented Jan 17, 2020

Do you want to request a feature or report a bug?
bug

What is the current behavior?
When trying to do a nested populate from the document's populate() method, nothing happens

If the current behavior is a bug, please provide the steps to reproduce.

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh8518`;
const connectionString = `mongodb://localhost:27017/${GITHUB_ISSUE}`;
const {Schema} = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
    const db = await mongoose.connect(connectionString);
    await mongoose.connection.dropDatabase();

    const workingSchema = mongoose.Schema({
        userApproval: {
            users: [
                {
                    user: {
                        type: mongoose.Schema.Types.ObjectId,
                        ref: 'Populated'
                    }
                }
            ]
        }
    });

    const failingSchema = mongoose.Schema({
        userApproval: {
            type: {
                users: [
                    {
                        user: {
                            type: mongoose.Schema.Types.ObjectId,
                            ref: 'Populated'
                        }
                    }
                ]
            },
            default: null
        }
    });

    const schemaToPopulate = mongoose.Schema({
        test: {
            type: String,
            default: "TEST"
        }
    });

    const W = db.model('Test', workingSchema);
    const F = db.model('TestFail', failingSchema);
    const P = db.model('Populated', schemaToPopulate);

    let p = await P.create({});
    let w = await W.create({userApproval: {users: [{user: p._id}]}});
    let f = await F.create({userApproval: {users: [{user: p._id}]}});
    await w.populate('userApproval.users.user', 'test').execPopulate();
    await f.populate('userApproval.users.user', 'test').execPopulate();
    console.log(w.userApproval.users);
    console.log(f.userApproval.users);
}

What is the expected behavior?
Even with a default: null for a property that is an object, the nested populate should still work.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 10.x
Mongoose 5.8.7 (.8.8 doesn't seem to be in the NPM repo) 5.8.9
MongoDB 4.0.9

@Ncifra Ncifra changed the title Nested populate doesn't work when the main property has a default: null definition Nested populate doesn't work when the main property has a default: null definition Jan 17, 2020
@vkarpov15 vkarpov15 added this to the 5.8.10 milestone Jan 23, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jan 23, 2020
@vkarpov15
Copy link
Collaborator

Your schema definition is incorrect. As written, your failingSchema defines userApproval as a mixed path, so the ref underneath the Mixed gets ignored. Do this instead:

    const failingSchema = mongoose.Schema({
        userApproval: {
            type: mongoose.Schema({
                users: [
                    {
                        user: {
                            type: mongoose.Schema.Types.ObjectId,
                            ref: 'Populated'
                        }
                    }
                ]
            }),
            default: null
        }
    });

More evidence for why we need something like #7181

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 25, 2020
@vkarpov15 vkarpov15 removed this from the 5.8.10 milestone Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants