【发布时间】:2017-06-21 12:35:03
【问题描述】:
"First" 与 Object t 一起打印,但在输入 Callback 之后的 Callback 与 t 一起打印为 undefined。在 Applied(t) 函数中,由于 Object t 出于某种原因此时未定义,因此会导致错误 -TypeError: Cannot read property '_id' of undefined-。如果在进入此回调函数之前没有未定义,这可能是什么原因? update() 是一个 MongoDB 函数。
function applied(t)
{
this.transactions.update(
{
_id: t._id, state: "pending" },
{
$set: { state: "applied" },
$currentDate: { lastModified: true }
}
)
}
function applytransaction(t,f,fb)
{
x=fb(t.value);
y=f(t.value);
this.model.update(
{ _id: t.source, pendingTransactions: { $ne: t._id } },
{ $inc: { bal:x }, $push: { pendingTransactions: t._id } }
, function(err,t,y) {
console.log("First "+t);
this.model.update(
{ _id: t.destination, pendingTransactions: { $ne: t._id } },
{ $inc: { bal: y }, $push: { pendingTransactions: t._id } }
, function(err, t) {
console.log("Second " +t);
applied(t);
});
});
}
【问题讨论】:
-
您有太多名为
t的变量。给他们唯一的名字。就此而言,请停止使用 1 和 2 个字母的变量名称,并为所有变量提供有意义的名称。 -
所以第二次更新失败,显然
t不是你想的那样
标签: javascript node.js mongodb