javascript - Date.now() not a number in Mongodb - Stack Overflow

This Meteor server code stores a property createdAt: Date.now() expecting a number which is the epoch t

This Meteor server code stores a property createdAt: Date.now() expecting a number which is the epoch time, but when I view the document in mongo shell, I get "ISODate(\"2016-12-25T22:31:09.553Z\")"

UsageCol.before.insert(function (userId, doc) {
  doc.userId = userId;
  doc.createdAt = Date.now();
  doc.period = new Date(doc.createdAt).getMonth() + 1 + '' + new Date(doc.createdAt).getFullYear();
});

Then, I wanted to change the date, so on the mongo shell I did:

db.users.update({'emails.0.address':'[email protected]'},{$set:{createdAt:'ISODate("2017-02-25T22:31:09.553Z")'}})

But now I get:

Exception while invoking method 'myMethod' TypeError: accMills.getMonth is not a function

let accMills = Meteor.user().createdAt;
let freeTime = accMills.setMonth(accMills.getMonth());

This Meteor server code stores a property createdAt: Date.now() expecting a number which is the epoch time, but when I view the document in mongo shell, I get "ISODate(\"2016-12-25T22:31:09.553Z\")"

UsageCol.before.insert(function (userId, doc) {
  doc.userId = userId;
  doc.createdAt = Date.now();
  doc.period = new Date(doc.createdAt).getMonth() + 1 + '' + new Date(doc.createdAt).getFullYear();
});

Then, I wanted to change the date, so on the mongo shell I did:

db.users.update({'emails.0.address':'[email protected]'},{$set:{createdAt:'ISODate("2017-02-25T22:31:09.553Z")'}})

But now I get:

Exception while invoking method 'myMethod' TypeError: accMills.getMonth is not a function

let accMills = Meteor.user().createdAt;
let freeTime = accMills.setMonth(accMills.getMonth());
Share Improve this question asked Mar 20, 2017 at 22:13 Fred J.Fred J. 6,04913 gold badges60 silver badges113 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you set createdAt as Date.now() it is not going to be set as a Date object but as a number that is the unix timestamp in milliseconds. So accMills.getMonth() is like 1490048577615.getMonth(): it doesn't make sense. Instead you should do new Date(accMills).getMonth()

If you want to save a date object, you should set createdAt as new Date():

UsageCol.before.insert(function (userId, doc) {
  doc.userId = userId;
  doc.createdAt = new Date();
  doc.period = doc.createdAt.getMonth() + 1 + '' + doc.createdAt.getFullYear();
});

Try this doc.createdAt = new Date().getTime() it will return the date into epoch time format

UPDATE: as @RobG ment Date.now() and new Date().getTime() is same.

Try this instead on set $set:{createdAt:new Date('ISODate("2017-02-25T22:31:09.553Z")')}

And n your freetime variable let freeTime = new Date(accMills).getMonth();

If you are trying to insert,update a new doc, use dateCreated : Date.now() from a js shell like intelliShell in STUDIO 3T for mongoDB which will give you a number as output.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790030a4593860.html

相关推荐

  • javascript - Date.now() not a number in Mongodb - Stack Overflow

    This Meteor server code stores a property createdAt: Date.now() expecting a number which is the epoch t

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信