2016-10-03 4 views
0

Ich verwende Ionic 2 mit Meteor/MongoDB.Meteor: Globales Objekt wird nicht aktualisiert

Wenn ich den folgenden Code, es inserts das chat Objekt in die localChatCollection:

 let promise: Promise<Mongo.Collection<Chat>> = this.findChats(); 
     promise.then((data: Mongo.Collection<Chat>) => { 

     let localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null); 
     data.find().forEach(function (chat: Chat) { 
      console.log('==> ' + chat); 
      localChatCollection.insert(chat); 
     }); 

Allerdings, wenn ich die localChatCollection global definieren, ist es nicht insert das chat Objekt. Es gibt keine Fehler, aber der Prozess stoppt nur auf der insert Linie.

private localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null); 
.... 
     this.localChatCollection.insert(chat); 

Alle Ideen, wie kann ich dies zu einem global collection definiert einzufügen bekommen?

Antwort

0

Dies funktioniert, aber ich glaube nicht, dass die eleganteste Lösung ist:

 let that = this; 
     data.find().forEach(function (chat: Chat) { 
      that.localChatCollection.insert(chat); 
     });