javascript - Callback generates "TypeError: this is undefined" in Angular2Firebase - Stack Overflow

I'm trying to understand what is gong on here and why I'm getting an error if I call a functi

I'm trying to understand what is gong on here and why I'm getting an error if I call a function a certain way and not getting the error when i call the function a different way. Here is the way that produces the error first:

player.service.ts file

in the @Injectable i have

private roomsRef: Firebase;

constructor() {
    this.roomsRef   = new Firebase("");
}

postGameActions(roomId: string) {
        console.log("post game actions start on " + roomId);

//error on below 'this'        
        this.roomsRef.child(roomId).child("board").once("value", snap => {
           let board = snap.val();
           //do stuff with 'board' object
        });
}

roomponent.ts file

activateTimer(roomId: string, roomName: string, callback) {
    var timer = setInterval(redirectTimer, 1000);
    var seconds: number = 5;
    var timerHtml = document.getElementById("divRedirectTimer" + roomName);
    timerHtml.innerHTML = "[" + seconds + "]";

    function redirectTimer() {
        timerHtml.innerHTML = "[" + (seconds--) + "]";
        if(seconds === 0) {
            clearInterval(timer);
            callback(roomId);
        }
    };
}

call non-working version like this

activateTimer(room.id, room.name, _playerService.postGameActions)

Error:

EXCEPTION: TypeError: this is undefined

Working version

Works fine when like this but no use of setInterval() since activateTimer just calls the service method

roomponent.ts file

activateTimer(roomId: string, roomName: string) {
    var timer = setInterval(redirectTimer, 1000);
    var seconds: number = 5;
    var timerHtml = document.getElementById("divRedirectTimer" + roomName);
    timerHtml.innerHTML = "[" + seconds + "]";

    function redirectTimer() {
        timerHtml.innerHTML = "[" + (seconds--) + "]";
        if(seconds === 0) {
            clearInterval(timer);
        }
    }

    this._playerService.postGameActions(roomId);

call working version like this

activateTimer(room.id, room.name)

Why is the 'this' undefined when i call postGameActions as a callback? I'm sure I'm missing something simple here

I'm trying to understand what is gong on here and why I'm getting an error if I call a function a certain way and not getting the error when i call the function a different way. Here is the way that produces the error first:

player.service.ts file

in the @Injectable i have

private roomsRef: Firebase;

constructor() {
    this.roomsRef   = new Firebase("https://xxx.firebaseio./rooms");
}

postGameActions(roomId: string) {
        console.log("post game actions start on " + roomId);

//error on below 'this'        
        this.roomsRef.child(roomId).child("board").once("value", snap => {
           let board = snap.val();
           //do stuff with 'board' object
        });
}

room.ponent.ts file

activateTimer(roomId: string, roomName: string, callback) {
    var timer = setInterval(redirectTimer, 1000);
    var seconds: number = 5;
    var timerHtml = document.getElementById("divRedirectTimer" + roomName);
    timerHtml.innerHTML = "[" + seconds + "]";

    function redirectTimer() {
        timerHtml.innerHTML = "[" + (seconds--) + "]";
        if(seconds === 0) {
            clearInterval(timer);
            callback(roomId);
        }
    };
}

call non-working version like this

activateTimer(room.id, room.name, _playerService.postGameActions)

Error:

EXCEPTION: TypeError: this is undefined

Working version

Works fine when like this but no use of setInterval() since activateTimer just calls the service method

room.ponent.ts file

activateTimer(roomId: string, roomName: string) {
    var timer = setInterval(redirectTimer, 1000);
    var seconds: number = 5;
    var timerHtml = document.getElementById("divRedirectTimer" + roomName);
    timerHtml.innerHTML = "[" + seconds + "]";

    function redirectTimer() {
        timerHtml.innerHTML = "[" + (seconds--) + "]";
        if(seconds === 0) {
            clearInterval(timer);
        }
    }

    this._playerService.postGameActions(roomId);

call working version like this

activateTimer(room.id, room.name)

Why is the 'this' undefined when i call postGameActions as a callback? I'm sure I'm missing something simple here

Share Improve this question asked Apr 23, 2016 at 18:51 ska.devska.dev 1691 silver badge9 bronze badges 1
  • How activateTimer is called? – Thierry Templier Commented Apr 23, 2016 at 19:35
Add a ment  | 

1 Answer 1

Reset to default 6

You need to wrap the call into an arrow function:

activateTimer(room.id, room.name, () => {
  _playerService.postGameActions();
});

The problem in your code is that you reference directly a function so you lose the object it will be executed on.

Another way would be to use the bind method:

activateTimer(room.id, room.name, _playerService.postGameActions.bind(_playerService);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742256215a4410067.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信