Javascript 获取Mongoose结果的对象中的值

Javascript 获取Mongoose结果的对象中的值,javascript,express,mongoose,Javascript,Express,Mongoose,我的查询如下:const result=await Form.findOne{{u id:req.params.id}; 下面是我查询的结果: 我正在尝试像这个控制台一样获取像素。logresult.formElement.pixel 但是,当我试图获取像素的值时,它返回未定义的值。 发生了什么? 使现代化 这是我的密码: 我尝试在不使用async而使用Promise的情况下编辑代码,但它也会返回未定义的您需要在这里进行一些挖掘。添加更多的console.log以调试实际发生的情况。首先,我建议

我的查询如下:const result=await Form.findOne{{u id:req.params.id}; 下面是我查询的结果:

我正在尝试像这个控制台一样获取像素。logresult.formElement.pixel 但是,当我试图获取像素的值时,它返回未定义的值。 发生了什么? 使现代化 这是我的密码:


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

您需要在这里进行一些挖掘。添加更多的console.log以调试实际发生的情况。首先,我建议打印出结果,看看它到底是什么。我的第一个猜测是你想在等待结束前打印结果?我不确定,因为我不知道你什么时候在没有看到代码的情况下打印任何东西。

你需要在这里做一些挖掘。添加更多的console.log以调试实际发生的情况。首先,我建议打印出结果,看看它到底是什么。我的第一个猜测是你想在等待结束前打印结果?我不确定,因为我不知道你什么时候在没有看到代码的情况下打印任何东西。

你需要使用exec函数,它会返回承诺

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
    // });
});
您需要使用exec函数,它返回承诺

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
    // });
});

调试结果.formElement.formName且无异步等待。返回类型错误:无法读取未定义@StoychoTrenchevDebug result.formElement.formName且无异步等待的属性“formName”。返回类型错误:无法读取未定义@Stoychotrenchevsvs的属性“formName”,直到返回未处理的Promisejection警告:未处理的承诺拒绝拒绝id:1:TypeError:无法读取undefinedresult=JSON.parseJSON.stringifyresult的属性“pixel”。请尝试此操作。它可以工作!你能解释一下为什么我需要使用JSON.parseJSON.stringifyresult吗?默认情况下,mongoose返回文档。选中此项仍返回未处理的PromisejectionWarning:未处理的承诺拒绝id:1:TypeError:无法读取undefinedresult=JSON.parseJSON.stringifyresult的属性“pixel”。请尝试此操作。它可以工作!你能解释一下为什么我需要使用JSON.parseJSON.stringifyresult吗?默认情况下,mongoose返回文档。检查这个
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
    // });
});