首页 > 解决方案 > 无法读取未定义 vue.js 的属性“查找”

问题描述

所以我试图从另一个服务获取数据并将其转换为对象,并通过 ID 获取特定属性,但问题是我的数据似乎未定义,也许语法错误?但我不会这么说...

app.get ('/products&comments/:title', async (req , res) => {
    
    const title = req.params.title;
    
    const db = await connect();

    const doc = await db.collection("posts").findOne({title: title});
    
    
    const { comments } = await axios.get("http://localhost:4201/comments");
    
    const singleProductComment = await comments.find((comments) => comments.postID === doc._id);

    console.log(singleProductComment)

   res.json(singleProductComment)


});

可能有什么问题,这种语法不是const singleProductComment = await comments.find((comments) => comments.postID === doc._id);很好吗?

完整的错误信息:

在此处输入图像描述

Console.log(comments) 输出:

在此处输入图像描述

标签: javascriptvue.jsaxios

解决方案


您的循环变量名称“comments”与数组变量名称“comments”相同,您也不需要在查找之前等待。

 **comments**.find((**comments**) => **comments**.postID === doc._id);

应该:

comments.find((comment) => comment.postID === doc._id);

这里: comments = [comment, comment, comment, comment];


推荐阅读