【发布时间】:2019-06-21 01:29:52
【问题描述】:
我正在尝试使用 express 作为后端通过其 id 获取 mongodb 文档。每次我请求路由时,findOne & populate 查询都会返回相同的文档。我想按 Id 返回患者数据。有人可以帮我解决这个问题吗?我是 JavaScript 新手。
// @ route GET api/profile/patient/:patientId
// @ desc Get patient by patientId
// @ access Private
router.get('/:patient_id', auth, async (req, res) => {
try {
const patient_profile = await Patient.findOne({
patient: req.params._id
}).populate('patient');
//console.log(patient);
console.log(patient_profile);
if (!patient_profile) return res.status(400).json({ msg: 'Patient not found' });
res.json(patient_profile);
} catch (err) {
console.error(err.message);
if (err.kind == 'ObjectId') {
return res.status(400).json({ msg: 'Profile not found' });
}
res.status(500).send('Server Error');
}
});
module.exports=router;
【问题讨论】:
-
req.params.patient_id? -
嗨 Ayush.. 该函数现在返回 null.. 请帮助
标签: node.js mongodb express mongoose