2016-10-03 66 views
0

我有一個聊天應用程序,即使用Ionic 2MeteorMongoDB。它完美的作品。添加數組對象到minimongo

但是,所有內容都存儲在服務器上的MongoDB中,因此每次用戶想要查看其消息時,都需要將其連接到運行在雲中的Meteor/Mongo服務器。另外,如果一個用戶刪除他們的chat,它將刪除MongoDB上的chat,並且對應的其他用戶也將刪除其chat

我想類似的功能WhatsApp的其中messages在設備上本地保存(我用SQLite),只有新messages在雲中,直到兩個用戶下載他們舉行。

目前我的應用程序遍歷一個Mongo.Cursor<Chat>對象。它也觀察到這個對象(this.chats.observe({changed: (newChat, oldChat) => this.disposeChat(oldChat), removed: (chat) => this.disposeChat(chat)});)。

我得到chat數據來自SQLlite,我已經存儲在本地(Array<Chat>)。

問題

是否有可能在SQLite數據(Array<Chat>)添加到Mongo.Cursor<Chat>?當我這樣做時,我只想在服務器上添加minimongo而不是MongoDB

感謝

UPDATE按照以下提醒

天冬氨酸,我做了以下內容:如果它工作

let promise: Promise<Mongo.Cursor<Chat>> = new Promise<Mongo.Cursor<Chat>>(resolve => { 
    this.subscribe('chats', this.senderId, registeredIds,() => { 
    let chats: Mongo.Cursor<Chat> = Chats.find(
     { memberIds: { $in: registeredIds } }, 
     { 
     sort: { lastMessageCreatedAt: -1 }, 
     transform: this.transformChat.bind(this), 
     fields: { memberIds: 1, lastMessageCreatedAt: 1 } 
     } 
    ); 

    this.localChatCollection = new Mongo.Collection<Chat>(null); 
    console.log(this.localChatCollection); 

    chats.forEach(function (chat: Chat) { 
     console.log('findChats(): add chat to collection: ' + chat); 
     this.localChatCollection.insert(chat); 
    }); 

將更新。

UPDATE

當我這樣做時,它insertschat對象:

 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

回答

1

是否有可能將SQLite數據(數組)添加到Mongo.Cursor?當我這樣做時,我只想添加到minimongo而不是服務器上的MongoDB。

流星本身對SQLite一無所知,但它聽起來像你有它的那部分工作。

要添加到minimongo而不是mongodb服務器,您正在尋找一個客戶端集合。就在該呼叫的第一個參數來創建你的收藏即

var localChatCollection = new Mongo.Collection(null) 

然後,您可以插入到localChatCollection你會與一個同步採集以同樣的方式傳遞null

Source: Meteor docs

+0

嗨cobberboy,感謝您的反饋意見。我沒有使用'Mongo.Collection',而是使用'Mongo.Cursor'。我在'Cursor'上使用'觀察'。可以用'Collection'做同樣的事情嗎? – Richard

+0

根據文檔,我無法「觀察」「集合」。所以我不認爲以上是合適的答案,除非我錯了? – Richard

+0

您的解決方案有效,謝謝。 – Richard