我只是不知道为什么它不起作用(( val.instruments 未定义。
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);
}
});
});
}
方案:
{
"_id": {
"$oid": "508677a3e5089a6df291631a"
},
"settings": "settings",
"instruments": [
"1",
"2",
"3"
]
}
返回:
node app.js:
from getInstruments undefined
有什么建议么?谢谢。
UPD:修改代码为:
function getInstruments(callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }).toArray(function(err, docs) {
console.dir(docs.instruments);
});
});
}
结果:
[ { _id: 508677a3e5089a6df291631a,
settings: 'settings',
instruments: [ '1', '2', '3' ] } ]
变成:
function getInstruments(callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }).toArray(function(err, docs) {
console.dir(docs.instruments);
});
});
}
结果:
undefined
怎么可能?