Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在发送头后设置头错误。节点JS_Javascript_Node.js_Post - Fatal编程技术网

Javascript Can';t在发送头后设置头错误。节点JS

Javascript Can';t在发送头后设置头错误。节点JS,javascript,node.js,post,Javascript,Node.js,Post,我不熟悉JavaScript和NodeJS。我有以下代码: app.post('/sendSensorsUserIds', function (req, res) { var myBody= req.body; var sensors= myBody.Sensors; var ids= myBody.ids; DButils.getSensorsUserIds(connection, ids , sensors , function(ids){ return res.se

我不熟悉JavaScript和NodeJS。我有以下代码:

app.post('/sendSensorsUserIds', function (req, res) {
  var myBody= req.body;
  var sensors= myBody.Sensors;
  var ids= myBody.ids;
  DButils.getSensorsUserIds(connection, ids , sensors , function(ids){
    return res.sendStatus(ids);
  });  
  res.end();
});
我总是遇到以下错误:

C:\Users\Daniel\Server\node_modules\mysql\lib\protocol\Parser.js:82
        throw err;
        ^

Error: Can't set headers after they are sent.

有人知道问题出在哪里吗?

这是因为您在执行res.send()后发送状态。尝试删除res.send()

在DButils.getSensorsUserIds中的回调之前调用res.end()。 我建议,

app.post('/sendSensorsUserIds', function (req, res) {
  var myBody= req.body;
  var sensors= myBody.Sensors;
  var ids= myBody.ids;
  DButils.getSensorsUserIds(connection, ids , sensors , function(ids){
    return res.status(ids).end();
  });  

});

可能的副本不需要
res,在底部结束。也许你在db查询处理中的意思是
res.json(ids)
,你确定我不需要res.end()结尾吗?@danieltheman如果你已经阅读了文档和node.js的概念,你一开始就不会问这个问题。这是一个帮助平台,如果你不想帮助,你不必。。。
app.post('/sendSensorsUserIds', function (req, res) {
  var myBody= req.body;
  var sensors= myBody.Sensors;
  var ids= myBody.ids;
  DButils.getSensorsUserIds(connection, ids , sensors , function(ids){
    return res.status(ids).end();
  });  

});