gpt4 book ai didi

node.js - 我的 postman 正在发出发送请求,但对某些值不执行任何操作

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:33 25 4
gpt4 key购买 nike

这是我在 Node JS 中的代码,我使用的是 mysql

router.post('/districts', (req, res) => {
const stateid = req.body.stateid;
db.query("select id, name from lkup_district where stateid = '"+stateid+"' ", (error, rows) => {
if(error) {
console.log(error);
}
if(rows.length>0) {
res.send(rows);
}
});
});

使用 stateid 我正在获取地区数据。但是,如果我发送一些 lkup_districts 中不存在的 stateid,Postman 就会给我加载,但什么也没有发生。出了什么问题?

我应该写

if(rows.length=0) {
}

最佳答案

看。仅当您发现某些内容时,您才会根据要求发送答案。如果发生错误或您的请求找不到任何内容,您不会发回任何数据。所以,恕我直言,您需要稍微修改您的代码。类似的东西

router.post('/districts', (req, res) => {
const stateid = req.body.stateid;
db.query(
"select id, name from lkup_district where stateid = ? "
,[stateid], (error, rows) => {
if(error) {
console.log(error);
res.status(500).send("Error proceeding query");
}
if(rows.length>0) {
res.send(rows);
}
else {
res.status(404).send("No data found");
}

}
);
});

关于node.js - 我的 postman 正在发出发送请求,但对某些值不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52305667/

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