0

On node.js I get an error:

TypeError: Cannot read property 'talk_duration' of undefined

From: setTimeout(this.hangup, this.data.talk_duration * 1000); in the setInterval.

However, outside of setInterval I have console.log(this.data.talk_duration); which work fine.

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);
    });
}
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213

1 Answers1

3

You need to remember this right as the top line in your oucomeAnswer function, e.g.: var that=this;

Then use that.data.talk_duration in functions with different scope.

Alex Pakka
  • 9,466
  • 3
  • 45
  • 69