我有一個聊天應用程序,即使用Ionic 2
和Meteor
與MongoDB
。它完美的作品。添加數組對象到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
當我這樣做時,它inserts
的chat
對象:
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
,它不insert
的chat
對象。沒有錯誤,但是這個過程只停留在insert
行。
private localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
....
this.localChatCollection.insert(chat);
任何想法如何,我能得到這個插入全局定義的collection
?
嗨cobberboy,感謝您的反饋意見。我沒有使用'Mongo.Collection',而是使用'Mongo.Cursor'。我在'Cursor'上使用'觀察'。可以用'Collection'做同樣的事情嗎? – Richard
根據文檔,我無法「觀察」「集合」。所以我不認爲以上是合適的答案,除非我錯了? – Richard
您的解決方案有效,謝謝。 – Richard