I am new in javascript and using nodejs and mongoose query to get a result which I would like to loop through and get some values in another array.
The resultset looks like
[ { _id: 5366e9049cfc825b32966852,
companies:
[ 5ab20bb69cb2754e949a09dc,
5ac5d53983d45353bc6a9c54,
5ac62eeca5421e4cb9abf63e]
},
{ _id: 5b9251f8ae8db624755f4b90,
companies:
[ 5b461c892bb9c81bd3ed4a25,
5b5086196947782fbc873d28,
5b76a6c79dc71a4a12564cc5 ]
}]
The final array should look like --
[ 5ab20bb69cb2754e949a09dc,
5ac5d53983d45353bc6a9c54,
5ac62eeca5421e4cb9abf63e,
5b461c892bb9c81bd3ed4a25,
5b5086196947782fbc873d28,
5b76a6c79dc71a4a12564cc5]
My code--
Model.find().exec(function(err,gdoc){
if(err)
{
callback({err:err,message:"Error looking up company"});
}else
{
for (var i = 0, len = gdoc.length; i < len; i++) {
console.log(gdoc.companies);
}
}
});
I am getting undefined values.
Any help is highly appreciated. Thanks in advance.