gpt4 book ai didi

javascript - 在 Mongoose 结果的对象中获取值

转载 作者:行者123 更新时间:2023-11-30 09:26:17 28 4
gpt4 key购买 nike

我有这样的查询 const result = await Form.findOne({ _id: req.params.id });
这是我的查询结果:

{ _id: 5a8ea110d7371b7e5040d5c9,
_user: 5a5c277f8d82ba3644b971c2,
formDate: 2018-02-22T10:53:04.579Z,
formElement:
{ formName: 'Vanilla Form',
pixel: '23423325',
fieldRows: [ [Object], [Object] ] },
formStyle:
{ globalColor: [ [Object], [Object], [Object] ],
elementSizing: 'medium',
elementStyle: 'radius',
fontFace: 'Open Sans',
fontSize: { label: '15', button: '15' } },
__v: 0,
status: true }

我正在尝试像这样获取像素 console.log(result.formElement.pixel)
但是,当我尝试获取像素的值时,它返回 undefined
怎么了?

更新
这是我的代码:

app.get("/view/:id", async (req, res) => {
const result = await Form.findOne({ _id: req.params.id });
console.log(result);
console.log(result.formDate); //return 2018-02-22T10:53:04.579Z,
console.log(result.formElement.pixel) //return undefined
// res.render("form", {
// result
// });
});

我尝试在不使用 async 的情况下编辑我的代码,而是使用 Promise,但它也返回 undefined

最佳答案

你需要使用exec()函数,它返回promise。

app.get("/view/:id", async (req, res) => {
let result = await Form.findOne({ _id: req.params.id }).exec();
result = JSON.parse(JSON.stringify(result));
console.log(result);
console.log(result.formDate); //return 2018-02-22T10:53:04.579Z,
console.log(result.formElement.pixel) //return 23423325
// res.render("form", {
// result
// });
});

关于javascript - 在 Mongoose 结果的对象中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930029/

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