0

issue.js

const mongoose;
const schema = new mongoose.Schema({
    docs: {
        type: [mongoose.Schema.Types.ObjectId],
        refPath: 'coll',
        required: true
    },
    coll: {
        type: String,
        required: true,
        enum: ['Product', 'Service']
    }
});

module.exports = mongoose.model('Issue', schema);

partial index.js

try{
    let issues = await Issue.find().populate('docs');

    console.log(issues);
    //every issue in issues, has an empty 'docs' array, when it should consist of products/services that exist

}catch(e){}

Not sure why arrays is making it not work, I tried making the docs field as singular ObjectId, and it worked fine. Only thing I could think of is that because it's an array, the refPath has to be different, but I'm not sure what I could add, as I tried using 'this.coll'.

1 Answer 1

1

I see an error in your schema. Not sure if that is the issue, but you can try.

const mongoose;
const schema = new mongoose.Schema({
    docs: [{
        type: mongoose.Schema.Types.ObjectId, // <-- This line
        refPath: 'coll',
        required: true
    }],
    coll: {
        type: String,
        required: true,
        enum: ['Product', 'Service']
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

That was to make it an array of ObjectIds, not a single 1. Oh but that helped me debug it! I had to do the Square Brackets around the whole docs object, not just the type! Thanks! :)
@DarrenA. Yeah. Updated my answer to support array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.