Thursday, 15 September 2011

node.js - Can't make populate in mongoose returning null -



node.js - Can't make populate in mongoose returning null -

im trying create fooditem schema in kept measureunit object reference page's model.

this measuretypesschema measureunit present. measuretypes embedded array document within metricsschema. exporting both measuretypesschema's model (measuretypes) , metricsschema's model (metrics).

var mongoose = require('mongoose'); schema = mongoose.schema; var measuretypesschema = new mongoose.schema({ measureunit:{type:string,required:true}, code:{type:string,required:true} }); var measuretypes = mongoose.model('measuretypes', measuretypesschema); module.exports = measuretypes; var metricsschema = new mongoose.schema({ organisation:{type: schema.types.objectid, ref: 'organisation'}, measuretypes:[measuretypesschema] }); var metrics = mongoose.model('metrics', metricsschema); module.exports = metrics;

this fooditemschema kept measureunit object reference measuretypes model nowadays in page

var mongoose = require('mongoose'); schema = mongoose.schema; var fooditemschema = new mongoose.schema({ measureunit:{type: schema.types.objectid, ref:'measuretypes',required:true}, }); var fooditem = mongoose.model('fooditem', fooditemschema); module.exports = fooditem;

i need details of measureunit when populate, how populate details of measureunit.

i tried populating measureunit fooditem , got null.

fooditem.findbyid(foodid).lean().populate({path:'measureunit'}).exec(function (err, food) { console.log(food);}

the result got populate

{ "_id": "5440d008b9215ace25587835", "measureunit": null, "__v": 0 }

node.js mongodb mongoose population

No comments:

Post a Comment