gpt4 book ai didi

javascript - promise 的意外结果

转载 作者:行者123 更新时间:2023-11-30 15:43:51 25 4
gpt4 key购买 nike

我正在尝试根据填充元素是否为 true || 来过滤填充元素false,然后我删除(或不删除)一些字段。

也就是说,我实际上是在复制带有意外响应的端点代码:

router.get('/endpoint', tokenAuth, function (req, res) {
var json_allData;
Model.getInfo(req.query.info)
.then(function (data) {
json_allData = data;
return Model.filterAnonymous(data.subdocument);
})
.then(function (data) { // << Data here is a subdocument filtered as expected
res.status(200).json({ data: json_allData }); // << WTF??? data has been filtered in the json_allData but I'm not doing anything!
})
.catch(function (error) {
res.status(400).json({ msg: error });
});
});

希望有人能在这里有所启发。

谢谢你的建议。

最佳答案

当你这样做的时候

json_allData = data;

您使 json_allData 指向与 data 相同的内存。这意味着两个变量引用同一个对象。

当你这样做的时候

Model.filterAnonymous(data.subdocument);

这会更改 subdocument 属性,该属性是 data 引用的对象的一部分,并且仍然是同一个对象 json_allData 正在引用。

关于javascript - promise 的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com