I've not used mongojs, but any db lookup will almost certainly be asynchronous. This means the function you passed to db.clusters.find
will not run immediately, but rather when the asynchronous call returns from mongo. Instead of returning a value from retrieveVertById
, try a callback function instead:
function retrieveVertById(id, successCallback) {
db.clusters.find({
id: id
}, function (err, clusters) {
if (err || !clusters) {
console.log("No cluster found");
} else {
var result = [];
clusters.forEach(function (cluster) {
vert = cluster["vertices"];
result.push(vert);
});
successCallback(result);
}
});
};
retrieveVertById("001", function(result) {
console.log(result);
});