如何在Meteor中系统地向新文档添加时间戳?



当一个新文档插入到Collection中时,我想给它添加一个时间戳。我希望服务器来做这件事,而不是客户端。这里的最佳解决方案是什么?

Rem:我宁愿不实现我自己的自定义Meteor.methods(),但使用经典的Meteor.Collection.insert()方法

from here - https://github.com/oortcloud/unofficial-meteor-faq

引用

如何在将每个文档添加到数据库之前更改它?

目前还不支持此功能,但您可以使用deny在服务器上实现您想要的功能。例如,给每个标记时间戳进入mongo:

Posts.deny({
  insert: function(userId, doc) {   
   doc.createdAt = new Date().valueOf();   
   return false; 
}}) 

' ' '

和Nate一样,我使用:

new Date().valueOf()

并将其附加到一个click事件,如下所示:

"click #messageSubmit": function (evt, templ) {
  var message = templ.find('#messageText').value;
  Messages.insert({
    message: message,
    createdAt: new Date().valueOf()
  });
}

我会用Date.now()。它看起来更干净,并且返回与new Date().getTime()相同的值。

new Date().getTime() === Date.now() // true

最新更新