返回值未定义

时间:2013-01-13 13:24:22

标签: javascript undefined

如果我在console.log中输出count + 1的输出,我得到一个正确的数字值。如果我输出note.note_id的值,我会得到undefined。这是为什么?

我已尝试将值设置为函数内的预定义值。

note.note_id = db.notes.count(function(err, count) {
  return count + 1;
});

1 个答案:

答案 0 :(得分:4)

很难回答而不知道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