2016-10-03 158 views
0

我正在使用Ionic 2与流星/ MongoDB。流星:不更新全局对象

当我这样做时,它insertschat对象为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); 
     }); 

但是,如果我定义全局localChatCollection,它不insertchat对象。没有错误,但是这个过程只停留在insert行。

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

任何想法如何让这个插入到全局定义collection

回答

0

这工作,但我不认为这是最好的解决方法:

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