How can I systematically add a timestamp to new documents in Meteor?

When a new document is inserted into the collection, I would like to add a timestamp to it. I want the server to do this, not the client. What is the best solution here?

Rem: I would rather not implement my own Meteor.methods() for this, but use the classic Meteor.Collection.insert() method

+4
source share
3 answers

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

Blockquote

How can I modify each document before adding it to the database?

There is currently no support, but you can use the option to achieve what you want on the server. For example, to timestamp each document as it goes in mongo:

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

`` ``

+7
source

Like Nate, to add the timestamp I used:

 new Date().valueOf() 

and attached it to a click event, for example:

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

I would use Date.now() . It looks cleaner and returns the same value as new Date().getTime() .

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

Source: https://habr.com/ru/post/1479178/


All Articles