我有一组api
,我将数据发布到数据库,作为响应,我从数据库中以json
形式获取所需的响应。我面临的问题是,当我第一次发布数据时,它会被发布,第二次我的服务器将崩溃并提供错误:发送后无法设置标头。我不知道为什么我会收到此错误。请让我知道我哪里出错了
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);
}
})
}
});
}
}
});
答案 0 :(得分:0)
因为db.query()
以异步方式工作,您遇到了这个问题。
所以,试试这个
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 i, count = 0;
// var j = 1;
var result = [];
for (i in skill) {
count++;
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) {
result.push(response);
response = {};
if(count == skill.length){
res.json(result);
}
} else {
db.query('select id,skill_id,skill_name,rating FROM skills WHERE skill_id = ?', [skillObj.skill_id], function(innerError, innerRows) {
response.success = true,
//response.skills = innerRows;
response.skills = skill;
response.mssg = 'Successfully Updated';
// I don't understand why you have used the variable 'j', so I commented it
// if(j === 1) {
// j = 0;
// res.json(response);
// }
result.push(response);
response = {};
if(count == skill.length){
res.json(result);
}
});
}
});
}
}
});
此外,当您嵌套db.query()
答案 1 :(得分:0)
错误:发送后无法设置标头。
具有简单的含义,即您多次发送res。
@SRK这里的错误就是这个
每次查询数据库时,您都会for loop
发送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);
});