2016-10-03 55 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); 
     });