javascript - 如何在express中的数组对象中找到对象
问题描述
对于每个帖子,我都有这样的结构:
{
"_id": "5dfd3b918937d40b98afd3f8",
"user": "5deea38cfc84f42590e01942",
"title": "test",
"description": "description test",
"category":
{
"0": "1",
"1": "101"
},
"phone": "+1",
"country": "USA",
"city": "NY",
"options": {
"transaction_type": ""
},
"date": "2019-12-20T21:22:25.940Z",
}
在快递中,我使用底部代码按类别查找帖子id
但没有工作我应该做什么?
router.get("/category/:category", async (req, res) => {
try {
const posts = await Post.find({
category: { $all: req.params.category }
}).sort({ date: -1 });
if (!posts) {
return res.status(404).json({ msg: "Ads not found" });
}
const resultPosts = posts.slice(req.query.start, req.query.count);
res.json(resultPosts);
} catch (err) {
console.error(err.message);
if (err.kind === "ObjectId") {
return res.status(404).json({ msg: "Ads not found" });
}
res.status(500).send("Server Error!");
}
});
解决方案
推荐阅读
- testing - 什么是测试中的需求覆盖率?
- python-3.x - tkinter 仅使用 after 刷新可见帧
- reactjs - 如何使用 reactjs 正确获取表单提交的消息计数
- javascript - 如何使用 Javascript 更有效地进行动态重复值验证?
- python - 不懂 Python 中的嵌套 For 循环
- openmaptiles - 如何从 Open Map Tiles 中获取适当的文件格式?
- php - Eloquent 查询单独工作,但不能在子查询中一起工作
- c - 重新创建 strstr()
- wordpress - Wordpress 网站下拉菜单突然停止工作
- javascript - 获取 XML 节点名称的 Javascript 循环不会停止