我有一个Node.js应用程序,正在使用Mongoose与Compose.io上的MongoDB进行接口。这是一些应将当前日期和时间存储在数据库中的代码:
signup.Volunteer.find({_id : uniqueid.toObjectId()}, function(err, doc){
var volunteer = doc[0];
var date = new Date();
console.log(date.toString());
//volunteer.time_out is an array
//defined in Volunteer Schema as: 'time_in : [Date]'
volunteer.time_in = volunteer.time_in.push(date);
volunteer.save(function(err){
...
});
});
....
如果我将这些日期对象打印到控制台,则会得到正确的日期。但是,当我将对象存储在数据库中时,它存储为“ 1970-01-01T00:00:00.001Z”。有什么想法为什么会这样吗?
最佳答案
问题是您要将volunteer.time_in.push
的返回值分配回volunteer.time_in
。返回值是数组的新长度,而不是数组本身。
因此,将该行更改为:
volunteer.time_in.push(date);