2016-10-12 56 views
0

我正在使用Ionic 2和Meteor/Mongo。Sorting a Meteor Cursor

我想排序Cursor,但發現它只是保持項目插入的原始順序。

模式

interface Chat { 
    _id?: string; 
    memberIds?: string[]; 
    title?: string; 
    subTitle?: string; 
    picture?: string; 
    lastMessage?: Message; 
    lastMessageCreatedAt?: Date; 
    receiverComp?: Tracker.Computation; 
    lastMessageComp?: Tracker.Computation; 
    } 

TS

private sortLocalChats(): void { 
    this.localChatCursor = this.localChatCollection.find({}, { sort: { lastMessageCreatedAt: -1 } }); 
    this.localChatCursor.forEach((chat: Chat) => { 
     console.log(chat.title+' '+chat.lastMessageCreatedAt); 
    }); 
    console.log('==> loaded sorted local chats: ' + this.localChatCollection.find().count()); 

輸出

Ashton Marais Thu Oct 06 2016 16:50:36 GMT+0800 (CST) 
Ashton Marais Wed Oct 12 2016 21:20:18 GMT+0800 (CST) 
ghjghj ghjghg Wed Oct 05 2016 23:37:49 GMT+0800 (CST) 
Brett Simpson Thu Oct 06 2016 23:52:05 GMT+0800 (CST) 
==> loaded sorted local chats: 4 

我本來期望這件T o按lastMessageCreatedAt排序。

任何幫助表示讚賞。

回答

0

要獲得排序結果,您可能需要先調用fetch()。所以在上面的例子中這個工作:

this.localChatCursor.fetch().forEach(...);