Saturday, 13 May 2017

Mongoose populate not always working

Given the following code, sometimes the criterias field is filed with data and sometimes it's not(Empty Array or filled with objects).

webSite.findOne({_id:id})
    .populate({
        path: 'createdBy webpages',
        populate: {
            path: 'createdBy criterias',
            populate: {
                path: 'createdBy'
            }
        }
    })
    .exec((err, doc) => ...here I'm checking the value

Schema:

const webSiteSchema = new mongoose.Schema({
        createdBy: {type: mongoose.Schema.Types.ObjectId, ref: 'users'},
        webpages: [{type: mongoose.Schema.Types.ObjectId, ref: 'webpages'}]
    });

Schema:

const webPageSchema = new mongoose.Schema({
        createdBy: {type: mongoose.Schema.Types.ObjectId, ref: 'users'},
        webpages: [{type: mongoose.Schema.Types.ObjectId, ref: 'webpages'}],
        criterias: [{type: mongoose.Schema.Types.ObjectId, ref: 'criterias'}]
    });

const WebPage = mongoose.model('webpages', webPageSchema, 'webpages');

Model

const Criteria = mongoose.model('criterias', criteriaSchema, 'criterias');



via Florian

No comments:

Post a Comment