我只是想要一个getapi,在那里我可以匹配req.params.id==post_id



下面是我的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 = 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
}

相关内容

最新更新