【发布时间】:2021-09-04 01:44:08
【问题描述】:
所以我试图从另一个服务获取数据并将其转换为对象,并通过 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(cmets) 输出:
【问题讨论】:
-
为什么不在api中实现
/comments?postId=呢?获取所有 cmets 然后在事后过滤它们似乎效率低下,如果它不是您的 API,那么您至少应该添加缓存 -
你到底是什么意思?
http://localhost:4201/comments?postId=doc._id?类似的东西? -
是的,所以它会按帖子 ID 过滤 cmets,如果它是您想要查找的特定评论,它将是
/comments/123,我只是指出获取所有 cmets 并不理想,想象你有 100k cmets,然后可能同时有 10 个人在你的网站上,服务器会拉一百万个东西只获取 10 个,更不用说说 1 条评论是 100 字节的 json,就像每个 API 请求 10MB,你应该尽可能多地卸载并传递参数 -
我大致知道你想说什么,你能更准确地回答我的问题吗?
标签: javascript vue.js axios