Meteor - 按时间戳排序 [英] Meteor - sorting by timestamp

查看:52
本文介绍了Meteor - 按时间戳排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个笔记应用程序,它允许用户添加笔记.我正在尝试在主页上呈现新创建的笔记,但是当我转到添加笔记并添加笔记,然后返回主页时,它显示新笔记(限制为 10)但新笔记是刚刚创建的是在列表的底部,而不是顶部.当我刷新页面时,它会像我想要的那样进入顶部.我将如何获得它以便我不必刷新页面即可在顶部获得新创建的笔记?

I have a note taking app which allows users to add notes. I am trying to render newly created notes on the home page, but when I go to the add note and add a note, then go back to the home page, it shows the new notes(limited to 10) but the new note that was just created is at the bottom of the list, instead of the top. When I refresh the page, it goes to the top like I want it to. How would I get it so that I don't have to refresh the page to get the newly created note at the top?

我尝试使用时间戳进行排序(使用 Date.parse(new Date())),然后使用它从服务器发布,

I try to sort by using a timestamp, (using Date.parse(new Date())), and then publishing from server using this,

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

这是我试图渲染它的地方:

Here is where I am trying to render it:

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);
})


}

推荐答案

您也需要对客户端进行排序.

You need to do the sort on the client as well.

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

这篇关于Meteor - 按时间戳排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆