Node.js 我只是想要一个get api,在这里我可以匹配req.params.id==post.\u id
下面是我发送请求的URL,请求的id保存在数据库中Node.js 我只是想要一个get api,在这里我可以匹配req.params.id==post.\u id,node.js,mongodb,express,mongoose,mongodb-query,Node.js,Mongodb,Express,Mongoose,Mongodb Query,下面是我发送请求的URL,请求的id保存在数据库中 http://localhost:4500/api/v1/get_comments/12345678 这在数据库中也可能是相同的结果 { "post_id": "12345678", "comment": "Top 10 SEO Training Institute" } 这是我的节点js代码 router.get('/get_comments/:id', async (req, res) => { const comment = a
http://localhost:4500/api/v1/get_comments/12345678
这在数据库中也可能是相同的结果
{
"post_id": "12345678",
"comment": "Top 10 SEO Training Institute"
}
这是我的节点js代码
router.get('/get_comments/:id', async (req, res) => {
const comment = await Comment.find({
$where: function () {
return (hex_md5(post.id) == req.params.id)
}
}).exec(function (err, result) {
if (err) {
let row = {
status: false,
status_code: 400,
message: 'There is some issue'
}
res.status(400).send(row);
} else {
let row = {
status: true,
status_code: 200,
data: result
}
console.log(result);
res.status(200).send(row)
}
})
})
如果数据库中存在post_id密钥,则只需执行以下操作:-
try{
const comment = await Comment.find({post_id:req.params.id})
//handle response here
} catch(err){
//handle error
}
如果数据库中存在post_id密钥,则只需执行以下操作:-
try{
const comment = await Comment.find({post_id:req.params.id})
//handle response here
} catch(err){
//handle error
}
您的问题是什么?如何使用Mongo DB where子句在数据库中存储的查询参数id制作get API?您的问题是什么?如何使用Mongo DB where子句在数据库中存储的查询参数id制作get API???@Harshvardhansingbaghel如果对您有效,请接受,因为这将帮助我对任何问题发表评论现在我只是直接写答案@HarshvardHansingbaghel如果这对你有用,请接受,因为它将帮助我对任何帖子发表评论,现在我只是直接写答案!