gpt4 book ai didi

javascript - Axios 和 Express : Sending params in Get request sends an empty params object to the backend

转载 作者:行者123 更新时间:2023-12-03 22:43:17 24 4
gpt4 key购买 nike

这是 Axios 请求 :

API.get("api/client/getClientByPK", { params: { id: 22 } });

这是 Express 路由器 :
router.get("/getClientByPK", function(req, res) {
logger.info("req.params", JSON.stringify(req.params));
clientServices.getClientByPK(req.params.id, res);
});

getClientByPk


var getClientByPK = function(clientPk, res) {
models.client
.findByPk(clientPk)
.then(clientFound => {
logger.debug("clientPk", clientPk);
logger.info("clientFound", JSON.stringify(clientFound));
if (clientFound != null) {
res.status(200).json(clientFound);
} else {
res.status(404).json({ error: "ClientNotFound" });
}
})
.catch(error => {
logger.error(error);
res.status(400).json(error);
});
};

这是 日志结果 :
2020-03-16T15:31:10+0100 <info> client_routes.js:12  req.params + {}
2020-03-16T15:31:10+0100 <debug> ClientServices.js:21 clientPk +
2020-03-16T15:31:10+0100 <info> ClientServices.js:22 clientFound + null

我已经使用 POSTMAN 测试了后端,它运行良好。所以我相信问题在于 AXIOS 请求
PS: 我在 SO 上只找到了一个答案,建议我使用数据而不是参数,但结果仍然相同。

最佳答案

Axios 发送 params 对象作为查询参数。

您应该通过 req.query 而不是 req.params 访问 express 中的查询参数

关于javascript - Axios 和 Express : Sending params in Get request sends an empty params object to the backend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com