I'm working with mongojs and I have to retrieve a field from an object taken from mongodb. I can not understand how to return the field:
function retrieveVertById(id){
var result = [];
db.clusters.find({id: id}, function (err, clusters){
if( err || !clusters) console.log("No cluster found");
else clusters.forEach( function (cluster) {
vert = cluster["vertices"];
result.push(vert);
console.log(result);
});
})
return result;
};
var a = retrieveVertById("001");
console.log(a);
The print inside the 'forEach' prints the correct value: (ex. [ [ [ 8, 2, 2 ], [ 2, 2, 5 ], [ 2, 2, 2 ], [ 5, 2, 2 ] ] ] ) On the contrary the print outside the cycle shows an empty array. what does not work with the return? Thanks in advance for your help.