I just don't know why it's not working(( val.instruments is undefinded.
function getInstruments(callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }, function (err, val) {
console.log('from getInstruments ' + val.instruments);
if (val.instruments==undefined) {
callback("");
} else {
callback(val.instruments);
}
});
});
}
scheme:
{
"_id": {
"$oid": "508677a3e5089a6df291631a"
},
"settings": "settings",
"instruments": [
"1",
"2",
"3"
]
}
returns:
node app.js:
from getInstruments undefined
Any suggestions? Thanks.
UPD: Modified code to:
function getInstruments(callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }).toArray(function(err, docs) {
console.dir(docs.instruments);
});
});
}
Result:
[ { _id: 508677a3e5089a6df291631a,
settings: 'settings',
instruments: [ '1', '2', '3' ] } ]
Changed to:
function getInstruments(callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }).toArray(function(err, docs) {
console.dir(docs.instruments);
});
});
}
Result:
undefined
How it can be?
find is returning an array. So you should check that val.length is greater than 0 and then look at val[0].instruments
Take a look at the example at the very end of the find section of the docs. If you run val.toArray()
within the callback to find
, you should be able to loop over the returned docs and process them one by one.