javascript - 无法读取未定义 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) 输出:
解决方案
您的循环变量名称“comments”与数组变量名称“comments”相同,您也不需要在查找之前等待。
**comments**.find((**comments**) => **comments**.postID === doc._id);
应该:
comments.find((comment) => comment.postID === doc._id);
这里: comments = [comment, comment, comment, comment];
推荐阅读
- android - Android - 如何在以编程方式隐藏软按钮后取消隐藏它们?
- jquery - jQuery文件上传插件在上传第二张图片时再次上传第一张图片
- react-native - 如何将领域与异步获取请求一起使用?
- java - JSP 在第一次执行时执行一个 java 类来设置后端
- ssis - SQL Server 和 SSIS 包中的版本兼容性问题
- blazor - 在同一页面上运行多个 Blazor 应用
- javascript - img比较滑块移动
- java - 为什么构造函数重载是编译时多态的一个例子?
- logstash - 多个 jdbc 输入是同时运行还是顺序运行?
- android - WorkManager 监听状态变化