如果我console.log count+1 的输出,我会得到一个正确的数值。如果我输出 note.note_id 的值,我会得到未定义的结果。这是为什么?
我尝试将值设置为函数内的预定义值。
note.note_id = db.notes.count(function(err, count) {
return count + 1;
});
最佳答案
如果不知道db.notes
是什么,就很难回答,但它似乎是访问数据库的东西。这意味着它很可能是异步的,这意味着 count()
方法永远不会返回值,但您需要在回调内对结果执行任何您想要执行的操作。
db.notes.count(function(err, count) {
note.note_id = count + 1;
// do more stuff here
});
// do NOT do stuff here. it will run BEFORE the callback has been executed
关于javascript - 返回值未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303930/