0
votes

here is my schema :

var sourcesSchema = {
    title: String,
    name: String,
    url: String,
    description: String,
    category: Array,
    rating: Number,
    source_pages: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'source_page',
    }]
}


var sourcePageschema = {
    uname: String,
    source_name: String,
    page_address: String,
    driver_name: String,
    product: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'products' //Edit: I'd put the schema. Silly me.
    }
}

var productsSchema = {
    title: String,
    uname: String,
    descriptin: String,
    images: Array,
    currency: String,
    last_update_time: Number,
    last_process_time: Number,
    meta_data: {},
    tags: Array,
    min_price: Number,
    max_price: Number,
    prices: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'prices' //Edit: I'd put the schema. Silly me.
    }]
}

this code works and populate the source_pages successfully :

_sources.find().populate('source_pages').exec(function (err,sources) {
    res.json(200, sources);
});

but if I want to populate the product too :

_sources.find().populate('source_pages').populate('source_pages.product').exec(function (err,sources) {
    res.json(200, sources);
})

this error :

TypeError: Cannot call method 'path' of undefined at search (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2088:28) at search (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2107:22) at Function._getSchema (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2114:5) at populate (/home/sina/rhino2/node_modules/mongoose/lib/model.js:1719:22) at Function.Model.populate (/home/sina/rhino2/node_modules/mongoose/lib/model.js:1702:5) at cb (/home/sina/rhino2/node_modules/mongoose/lib/query.js:1690:11) at /home/sina/rhino2/node_modules/mongoose/lib/utils.js:414:16 at /home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:158:16 at commandHandler (/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:643:16) at null. (/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1641:20)

1

1 Answers

2
votes

I was just hunting down the same problem, and I believe what you are looking for is this Mongoose: deep population (populate a populated field).

Basically, you are not able to do what you are trying to do, unless you do it in your callback function and then insert it in your return. I was trying to avoid that, but at the moment it seems like the only option. The other option, if you plan on doing a lot of this type of stuff, is to look into using a Relational DB.