Javascript Object not reached


#1

I have a Javascript problem I am a little confused about. I have a ts file with the following:

private localMessageCollection: Mongo.Collection<Message> = new Mongo.Collection<Message>(null);
...
      let that = this;
      this.subscribe('messages', this.activeChat._id, this.senderId, () => {
        this.autorun(() => {              
          let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
          promiseMessages.then((messageData: Mongo.Collection<Message>) => {
            console.log('find messageData: ' + messageData);
            messageData.find().forEach(function (message: Message) {
              console.log('find that.localMessageCollection.insert(message): ' + message);
              that.localMessageCollection.insert(message);
              console.log('done!!!');
            });
          });
        });
      });

My problem is that the following line doesn’t get executed. I would expect ‘done!!!’ to be printed.

that.localMessageCollection.insert(message);

output

find messageData: [object Object]
find that.localMessageCollection.insert(message): [object Object]

It has something to do with the this object scope I think, because if I add the following line above it, it runs.

let localMessageCollection: Mongo.Collection<Message> = new Mongo.Collection<Message>(null);
localMessageCollection.insert(message);

Any suggestions appreciated.

Compiled code:

    this.localMessageCollection = new mongo_1.Mongo.Collection(null);
    ....
    MessagesPage.prototype.loadMessages = function () {
        var _this = this;
        var promise = this.utilityService.getLoggedInPerson();
        if (promise) {
            promise.then(function (data) {
                _this.personModelLoggedIn = JSON.parse(data);
                if (_this.personModelLoggedIn) {
                    _this.senderId = 'P' + _this.personModelLoggedIn.id;
                    _this.title = _this.activeChat.title;
                    _this.picture = _this.activeChat.picture;
                    var that_1 = _this;
                    _this.subscribe('messages', _this.activeChat._id, _this.senderId, function () {
                        _this.autorun(function () {
                            var promiseLocalMessages = _this.findLocalMessages();
                            promiseLocalMessages.then(function (messageData) {
                                console.log('local messageData: ' + messageData);
                                messageData.find().forEach(function (message) {
                                    console.log('local that.localMessageCollection.insert(message): ' + message);
                                    that_1.localMessageCollection.insert(message);
                                });
                            });
                            var promiseMessages = _this.findMessages();
                            promiseMessages.then(function (messageData) {
                                console.log('find messageData: ' + messageData);
                                messageData.find().forEach(function (message) {
                                    console.log('find that.localMessageCollection.insert(message): ' + message);
                                    //console.log(that.localMessageCollection);
                                    that_1.localMessageCollection.insert(message);
                                    //console.log(that.localMessageCollection);
                                });
                            });
                        });
                    });
                }
                else {
                    _this.nav.push(login_1.LoginPage, {
                        fromMessages: true
                    });
                }
            });
        }
        else {
            this.nav.push(login_1.LoginPage, {
                fromMessages: true
            });
        }
    };