我无法替换 mongoose 返回的数组的值
I can't replace the value of an array returned by mongoose
我有一个 notifications.find 查询 (mongoose) returns 多张
Notification.find({id: id}, function(err, notifications){}
我需要像那样转换返回的时间戳值
[
{
_id: new ObjectId("12934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696599,
},
{
_id: new ObjectId("11934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696600,
}
]
我试过了
notifications.forEach((element, index) => {
notifications[index].timestamp = new Date(notifications[index].timestamp);
});
和这段代码
new Date(notifications[index].timestamp);
似乎可以通过转换每个值的时间戳来工作,但我无法在数组中替换它
我已经在这个问题上思考了几个小时
而不是 forEach
使用 map
const newNotifications = notifications.map((element, index) => {
return {
...element,
timestamp: new Date(element.timestamp),
}
});
我有一个 notifications.find 查询 (mongoose) returns 多张
Notification.find({id: id}, function(err, notifications){}
我需要像那样转换返回的时间戳值
[
{
_id: new ObjectId("12934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696599,
},
{
_id: new ObjectId("11934193c51a231b0165425a"),
userid: '62921df1c14a2eea0efa9399',
timestamp: 1653817696600,
}
]
我试过了
notifications.forEach((element, index) => {
notifications[index].timestamp = new Date(notifications[index].timestamp);
});
和这段代码
new Date(notifications[index].timestamp);
似乎可以通过转换每个值的时间戳来工作,但我无法在数组中替换它
我已经在这个问题上思考了几个小时
而不是 forEach
使用 map
const newNotifications = notifications.map((element, index) => {
return {
...element,
timestamp: new Date(element.timestamp),
}
});