在node.js上,我得到一个错误:
TypeError: Cannot read property 'talk_duration' of undefined出发地:setTimeout(this.hangup, this.data.talk_duration * 1000); in the setInterval。
但是,在setInterval之外,我有一个工作良好的console.log(this.data.talk_duration);。
this.outcomeAnswer = function () {
console.log(this.data.talk_duration); //this work
num = 0;
db.run("INSERT INTO in_queue (action_id, state) VALUES ('" + this.data.action_id + "', 'InQueue')", function (error) {
queueCheckLoop = setInterval(function () {
num++;
if (num == 5) {
clearInterval(queueCheckLoop);
}
db.each("SELECT count(*) as total FROM agent_queue WHERE state = 'Ready'", function (err, row) {
if (row.total > 0) {
clearInterval(queueCheckLoop);
setTimeout(this.hangup, this.data.talk_duration * 1000);
}
});
}, 1000);
});
}发布于 2015-08-25 15:24:19
您需要正确地记住this作为您的oucomeAnswer函数的顶部一行,例如:var that=this;
然后在不同作用域的函数中使用that.data.talk_duration。
https://stackoverflow.com/questions/32207924
复制相似问题