gpt4 book ai didi

mysql - Nodejs服务器崩溃问题

转载 作者:行者123 更新时间:2023-11-29 18:12:12 24 4
gpt4 key购买 nike

我有一组api,并且我将数据发布到数据库,作为响应,我从数据库中以json 形式获取所需的响应。我面临的问题是,当我第一次发布数据时,它将被发布,第二次我的服务器将崩溃并给出错误:发送后无法设置 header 。我不知道为什么我会收到此错误。请告诉我哪里出错了

router.post('/addcustomerskills', function(req,res) {
var skill = req.body.skill;
var skill_id = req.body.skill_id;
var skill_name = req.body.skill_name;
var rating = req.body.rating;
var response = {};

if (typeof skill ==='undefined') {
response.token = '2ewerr4';
response.success = false,
response.skill = null;
response.mssg = 'fields cannot be empty';
res.json(response);
} else {
var j =1;
for (i in skill) {
var skillObj=skill[i];
db.query('INSERT into skill(skill_id,skill_name,rating) values(?,?,?)', [skillObj.skill_id, skillObj.skill_name, skillObj.rating], function(error, rows) {
if (error) {
res.json(error);
} else {
db.query('select id,skill_id,skill_name,rating FROM skills WHERE skill_id = ?', [skillObj.skill_id], function(error, rows) {
response.success = true,
//response.skills = rows;
response.skills = skill;
response.mssg = 'Successfully Updated';
if(j === 1) {
j = 0;
res.json(response);
}
})
}
});
}
}
});

最佳答案

错误:发送后无法设置 header 。

含义很简单,即您多次发送资源。

@SRK 这里的错误是这样的

您有 for 循环,每次查询数据库时都会在其中发送 res

for 循环完成后发送回响应。

router.post('/addcustomerskills', function(req,res) {
var skill = req.body.skill;
var skill_id = req.body.skill_id;
var skill_name = req.body.skill_name;
var rating = req.body.rating;
var response = {};

if (typeof skill ==='undefined') {
response.token = '2ewerr4';
response.success = false,
response.skill = null;
response.mssg = 'fields cannot be empty';
res.json(response);
} else {
var j =1;
for (i in skill) {
var skillObj=skill[i];
db.query('INSERT into skill(skill_id,skill_name,rating) values(?,?,?)', [skillObj.skill_id, skillObj.skill_name, skillObj.rating], function(error, rows) {
if (error) {

} else {
db.query('select id,skill_id,skill_name,rating FROM skills WHERE skill_id = ?', [skillObj.skill_id], function(error, rows) {
response.success = true,
//response.skills = rows;
response.skills = skill;
response.mssg = 'Successfully Updated';
if(j === 1) {
j = 0;

}
})
}
});
}
res.json({"message":"what you want to send"});
}
});

如果你想处理crush,请将其添加到你的主node.js 文件中

process.on('uncaughtException', (err) => {
console.log("error",error);
});

关于mysql - Nodejs服务器崩溃问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47301879/

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