node.js - Mongoose returns null array when populate -


i'm trying create webapp mongoose , nodejs. first time i'm using mongoose , nodejs i'm not quit @ it.

i have person model:

var person = new mongoose.schema({     name: {type: string, required: true},     zipcode: {type: string, required: true},     city: {type: string, required: true},     street: {type: string, required: true},     address: {type: number, required: true},     email: {type: string, required: true},     type: {type: string, enum: ['lid', 'passant'], required: true},     ships: {       type: [{         name: {type: string, required: true},         length: {type: number, required: true},         type: {type: string, required: true},         picture: {type: buffer, required: false}       }],       required: false     },     user: {       type: {         username: {type: string, required: true},         password: {type: string, required: true},         passwordkey: {type: string, required: true},         roles: {type: [{type: mongoose.schema.types.objectid, ref: 'role'}], required: true}       },       required: false     }   },   {     collection: 'person'   }); 

and rol model:

var role = new mongoose.schema({     name: {type: string, required: true}   },   {     collection: 'role'   }); 

when i'm trying run find query on person model, want populate user.roles array. result remains empty.

 / specific person id or type/   server.get('/api/person/:id', function (req, res, next) {     if (helper.isvalidobjectid(req.params.id)) {       person.findbyid(req.params.id).populate("user.roles").exec(function (err, result) {           if (err) {             res.status(404).json(err);           } else {             res.status(200).json(result);           }         }       );     } else {       next();     }   }, function (req, res, next) { person.find({type: req.params.id}).populate('user.roles').exec(function (err, result) {   if (err) {     res.status(404).json(err);   } else {     res.status(200).json(result);   } }); 

});

and result get:

{      "_id":"553f79d4f1481c0c14b42d59",    "name":"wilco boogert",    "zipcode":"4305rh",    "city":"ouwerkerk",    "street":"baalpapenweg",    "address":2,    "email":"wilcoboogert17@gmail.com",    "type":"lid",    "user":{         "username":"wboogert",       "password":"sha1$168fd599$1$502b965cb083ebdfcafb17e655455ef63779e1a1",       "passwordkey":"kfjvlksdfm",       "roles":[          ]    },    "__v":0,    "ships":[         {            "name":"titanic",          "length":269,          "type":"olympic-klasse",          "_id":"553f79d4f1481c0c14b42d5a"       }    ] } 

the orginal object in database is:

{     "_id": {         "$oid": "553f79d4f1481c0c14b42d59"     },     "name": "wilco boogert",     "zipcode": "4305rh",     "city": "ouwerkerk",     "street": "baalpapenweg",     "address": 2,     "email": "wilcoboogert17@gmail.com",     "type": "lid",     "user": {         "roles": [              {             "$oid": "5522996f0fff331ed03cae6c"              }         ],         "passwordkey": "kfjvlksdfm",         "password": "sha1$168fd599$1$502b965cb083ebdfcafb17e655455ef63779e1a1",         "username": "wboogert"     },     "ships": [         {             "name": "titanic",             "length": 269,             "type": "olympic-klasse",             "picture": "<binary data>",             "_id": {                 "$oid": "553f79d4f1481c0c14b42d5a"             }         }     ],     "__v": 0 } 

so see roles empty. , search web cannot find problem is. can annyone me?

you should replace line :

roles: {type: [{type: mongoose.schema.types.objectid, ref: 'role'}], required: true} 

with 1 :

roles: {type: [{type: mongoose.schema.types.objectid, ref: 'role'}], required: true} 

in fact, reference must exact same name it's defined :

collection: 'role' 

it's case sensitive.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -