const allocation_me = async (request, response) => {
try {
const { user: userid } = request;
if (!ObjectId.isValid(userid)) throw new Error('invalid objectid');
const now = moment().format();
const date = new Date(now);
const allocation = await Allocation.findOne({ $and: [{ user: userid, start_date: { $lt: date }, end_date: { $gt: date } }] })
.populate('user', 'name')
.populate('garden');
if (!allocation) throw new Error('invalid request');
allocation.timestamp = moment(allocation.end_date).format('x');
response.status(200).send(allocation);
} catch (error) {
response.status(400).send(error);
}
};
我正在尝试将时间戳添加到mongo查询返回的对象,但是当它发送分配作为响应时,时间戳不会显示。我已经尝试记录了allocation.timestamp值,但它也没有显示,就像javascript忽略了我分配它一样。我尝试将const更改为let,但是显然这不是问题。
答案 0 :(得分:3)
...就像javascript忽略了我分配它一样。
如果分配对象是MongoDB的sealed或frozen,那完全有可能。
相反,制作副本并将您的媒体资源添加到副本中,也许会传播ES2018:
allocation = {...allocation, timestamp: moment(allocation.end_date).format('x')};
...或者如果您不能使用属性传播,请Object.assign
:
allocation = Object.assign({}, allocation, {timestamp: moment(allocation.end_date).format('x')});
在这两种情况下,您都需要将const
更改为let
,因为我们要更改变量allocation
持有的值。或者,当然,将其保留为const
并分别记住修改后的版本:
const updatedAllocation = {...allocation, timestamp: moment(allocation.end_date).format('x')};
response.status(200).send(updatedAllocation);
我尝试将const更改为let,但这显然不是问题所在。
正确。 const
适用于变量(allocation
),而不适用于变量引用的对象。
答案 1 :(得分:1)
从查询返回的文档是mongoose / mongodb文档,而不是普通的javascript对象。
因此,要修改返回文档中的任何内容,您必须将其分配给fn main() {
#![allow(path_statements)]
let init: fn(init::Peripherals) = init;
rtfm::atomic(unsafe { &mut rtfm::Threshold::new(0) },
|_t|
unsafe {
let _late_resources =
init(init::Peripherals{core:
::stm32f407::CorePeripherals::steal(),
device:
::stm32f407::Peripherals::steal(),});
});
let idle: fn() -> ! = idle;
idle();
}
,也可以使用.lean()
选项将猫鼬对象转换为纯JavaScript对象。
new Object()