2017-09-05 86 views
1

我有一个记事本应用程序,允许用户添加笔记。我试图在主页上呈现新创建的笔记,但是当我去添加笔记并添加笔记时,然后回到主页,它会显示新笔记(限制为10),但是新注释是刚刚创建的是位于列表的底部,而不是顶部。当我刷新页面时,它会像我想要的那样进入顶部。我怎样才能得到它,这样我就不必刷新页面来获取顶部新创建的注释了?流星 - 按时间戳排序

我试图通过使用时间戳(使用Date.parse(new Date())),然后使用这个从服务器发布到排序,

Meteor.publish('notes-newest', function() { 
    return Notes.find({},{sort: {createdAt: -1}, limit: 10}); 
}); 

这里就是我试图使它:

componentDidMount() { 
Meteor.subscribe('notes-newest') 
this.tracker = Tracker.autorun(() => { 
    const notes = Notes.find().fetch() 
    if(notes == null || notes == undefined){ 
    return; 
    } 
    this.setState({ notes }); 
    console.log(notes); 
}) 


} 
+0

[排序和限制的可能的复制不工作与Mongo +流星](https://stackoverflow.com/questions/45957640/sort-and-limit-not-working-with-mongo-meteor) – Styx

+0

当你插入你的笔记,我建议你设置字段用'new Date()'而不是'Date.parse(new Date())'创建。它给你更多的灵活性,使它存储日期对象,你仍然可以做这种排序。 –

回答

1

您还需要在客户端进行排序。

this.tracker = Tracker.autorun(() => { 
    const notes = Notes.find({},{sort: {createdAt: -1}).fetch() 
0

如果你仍然不,只要你想,那么你可以试试下面的代码

this.tracker = Tracker.autorun(() => { 
    const notes = Notes.find({},{sort: {createdAt: -1}).fetch() 
    var data = notes.reverse() 

你会得到降序排列您的数据得到的数据同时显示