node.js - How do I populate mongoose schema values from a schema referenced from inside another schema in Nodejs? -


i have mongoose schema of "products" includes array of colors(another schema). orders schema contains user , array of products. products contain product, quantity , color. color here schema specific product.

i want user able order product of specific color (color available product) how populate color of selected product ?

models\product.js

var mongoose = require('mongoose'), schema = mongoose.schema;  var colorschema = new schema({     value : {type : string}  });   var productschema = new schema({     name:  {         type: string,      default: ''     },     colors : [colorschema]   });   module.exports = mongoose.model('product', productschema);   module.exports = mongoose.model('color', colorschema ); 

models\order.js

var mongoose = require('mongoose'),     schema = mongoose.schema;  var orderschema = new schema({     timestamp : { type: date, default: date.now },     user : {type: mongoose.schema.types.objectid, ref: 'user'} ,     products : [{         product : {type: mongoose.schema.types.objectid, ref: 'product'},         color :  {type: mongoose.schema.types.objectid, ref: 'color'},         quantity : number         }]  });   module.exports = mongoose.model('order', orderschema ); 

the query :

function getorders(res) {     order.find({})     .populate('user')     .populate('products.product')     .populate('products.color')     .exec(function (err, orders) {         // if there error retrieving, send error. nothing after res.send(err) execute         if (err) {             //console.log("error");             res.send(err);         }         res.json(orders); // return orders in json format     });  } 

i have added entry db using postman . query returns color field null.

[   {     "_id": "5749b94508e742484c13d16e",     "__v": 0,     "user": {           "_id": "57482af31d885d030064f6ff",           "__v": 0,           "phone": "9999999999",           "name": "xyz"         },     "products": [       {         "product": {           "_id": "574957c3935ad71048593879",           "__v": 0,           "colors": [             {               "value": "black",               "_id": "574957c3935ad7104859387d"             },             {               "value": "gold",               "_id": "574957c3935ad7104859387c"             },             {               "value": "silver",               "_id": "574957c3935ad7104859387b"             }           ],           "name": "xiaomi mi note 3"         },         "color": null,       // null if put .populate('products.color')         "quantity": 5,         "_id": "5749b94508e742484c13d16f"       }     ],     "timestamp": "2016-05-28t15:29:09.416z"   } ] 

if remove .populate('products.color') , returns id of color present in product.

the user , product populated fine. issue color, since referenced inside product.

[   {     "_id": "5749b94508e742484c13d16e",     "__v": 0,     "products": [       {         "product": {           "_id": "574957c3935ad71048593879",           "__v": 0,           "colors": [             {               "value": "black",               "_id": "574957c3935ad7104859387d"             },             {               "value": "gold",               "_id": "574957c3935ad7104859387c"             },             {               "value": "silver",               "_id": "574957c3935ad7104859387b"             }           ],           "name": "xiaomi mi note 3"         },         "color": "574957c3935ad7104859387b",  // <-- id of silver color product if remove .populate('products.color')         "quantity": 5,         "_id": "5749b94508e742484c13d16f"       }     ],     "timestamp": "2016-05-28t15:29:09.416z"   } ] 

i want color field populated as:

"color": {   "value": "silver",   "_id": "574957c3935ad7104859387b" } 

any suggestions on ?

edit:

the code add order below:

exports.addorder = function (req, res) {      var prods_arr = [];      for(var = 0, l = req.body.products.length; < l; i++) {          prods_arr.push({             product: req.body.products[i].product,             color: req.body.products[i].color,             quantity: req.body.products[i].quantity             })         }          var order_obj = new order();     order_obj.user = req.body.user;     order_obj.products = prods_arr;      order.create(order_obj, function (err, order) {         if (err){             res.send(err);         }         // , return orders after create         getorders(res);     });  }; 

using postman , send below raw json data body of request.

{     "user": {           "_id": "57482af31d885d030064f6ff",           "__v": 0,           "phone": "9999999999",           "name": "xyz"         },     "products": [       {         "product": {           "_id": "574957c3935ad71048593879",           "__v": 0,           "colors": [             {               "value": "black",               "_id": "574957c3935ad7104859387d"             },             {               "value": "gold",               "_id": "574957c3935ad7104859387c"             },             {               "value": "silver",               "_id": "574957c3935ad7104859387b"             }           ],           "name": "xiaomi mi note 3"         },         "color": {               "value": "silver",               "_id": "574957c3935ad7104859387b"             },                "quantity": 5,       }     ]   } 


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -