【发布时间】:2021-03-21 11:24:42
【问题描述】:
我对 Node.js 有点陌生,并且在 Express.js 中编写了一些 GET REST API。对于我的一个 API,我需要传递 2 个参数来从数据库中获取数据。我已经使用单个参数实现了它,但不超过 2 个。有人帮帮我吗?
这是我的功能
getByNPPTahunBulan(req,res){
console.log(req.param.tahun_bulan)
Iuran_Dapen.findAll({
where: {
tahun_bulan: req.params.tahun_bulan,
npp: req.param.npp
}
}).then((data) => {
return res.send(data);
}).catch((err) => {
res.status(500).send({
message: err.message || 'Some error occurred while retrieving data.',
});
})
},
还有网址router.get('/iuranterakhir/:tahun_bulan/:npp', iuranDapen.getByNPPTahunBulan);
【问题讨论】:
-
你想在数据库中使用多参数还是在请求中使用多参数
-
req.param.npp必须是req.params.npp,这仅仅是一个错字造成的。您在tahun_bulan中正确使用了params,但忘记了npp中的“s”。您还忘记了console.log()函数调用中的“s”。
标签: javascript node.js api express get