admin管理员组

文章数量:1289582

I khow like a mon question here on. But I couldn't get a solution from everywhere. here is my code: if row is not empty then render code page, otherwise perform another action.

app.get('/send',function(req,res){
  var code=req.query['c']; // -- get request from input
  connection.query("use mynum");
  var strQuery = "select * from table WHERE code='"+code+"' LIMIT 1";   
  connection.query( strQuery, function(err, rows){
    if(err) {
      throw err;
    }else{
      if(rows.length==1){
        res.render('pages/code', {code : rows[0].code});
        connection.end();
        res.end();
      }else {
        // here is some actions
      }
    }
  });
  res.end();
});

the stack trace:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (C:\wamp\www\vin_number\node_modules\express\lib\re
sponse.js:666:10)
at ServerResponse.res.contentType.res.type (C:\wamp\www\vin_number\node_modu
les\express\lib\response.js:532:15)
at ServerResponse.send (C:\wamp\www\vin_number\node_modules\express\lib\resp
onse.js:121:14)
at fn (C:\wamp\www\vin_number\node_modules\express\lib\response.js:900:10)
at View.exports.renderFile [as engine] (C:\wamp\www\vin_number\node_modules\
ejs\lib\ejs.js:323:3)
at View.render (C:\wamp\www\vin_number\node_modules\express\lib\view.js:93:8
)
at EventEmitter.app.render (C:\wamp\www\vin_number\node_modules\express\lib\
application.js:530:10)
at ServerResponse.res.render (C:\wamp\www\vin_number\node_modules\express\li
b\response.js:904:7)
at Query._callback (C:\wamp\www\vin_number\server.js:102:6)

I khow like a mon question here on. But I couldn't get a solution from everywhere. here is my code: if row is not empty then render code page, otherwise perform another action.

app.get('/send',function(req,res){
  var code=req.query['c']; // -- get request from input
  connection.query("use mynum");
  var strQuery = "select * from table WHERE code='"+code+"' LIMIT 1";   
  connection.query( strQuery, function(err, rows){
    if(err) {
      throw err;
    }else{
      if(rows.length==1){
        res.render('pages/code', {code : rows[0].code});
        connection.end();
        res.end();
      }else {
        // here is some actions
      }
    }
  });
  res.end();
});

the stack trace:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (C:\wamp\www\vin_number\node_modules\express\lib\re
sponse.js:666:10)
at ServerResponse.res.contentType.res.type (C:\wamp\www\vin_number\node_modu
les\express\lib\response.js:532:15)
at ServerResponse.send (C:\wamp\www\vin_number\node_modules\express\lib\resp
onse.js:121:14)
at fn (C:\wamp\www\vin_number\node_modules\express\lib\response.js:900:10)
at View.exports.renderFile [as engine] (C:\wamp\www\vin_number\node_modules\
ejs\lib\ejs.js:323:3)
at View.render (C:\wamp\www\vin_number\node_modules\express\lib\view.js:93:8
)
at EventEmitter.app.render (C:\wamp\www\vin_number\node_modules\express\lib\
application.js:530:10)
at ServerResponse.res.render (C:\wamp\www\vin_number\node_modules\express\li
b\response.js:904:7)
at Query._callback (C:\wamp\www\vin_number\server.js:102:6)
Share Improve this question edited Dec 26, 2014 at 17:08 mscdex 107k15 gold badges200 silver badges158 bronze badges asked Dec 26, 2014 at 15:38 Munkhbat MygmarsurenMunkhbat Mygmarsuren 1131 gold badge2 silver badges13 bronze badges 2
  • Sorry, what exactly is the problem you're running into / the errors you're getting? "Can't set headers after they are sent on express," but it seems like they only get sent in one of two scenarios, and if rows.length !== 1 you should be able to write headers. Can you include that part of the code, as you may be writing the headers incorrectly – Keenan Lidral-Porter Commented Dec 26, 2014 at 15:55
  • What's the stack trace? – Farid Nouri Neshat Commented Dec 26, 2014 at 16:09
Add a ment  | 

2 Answers 2

Reset to default 7

You're sending a response twice via res.end(). Get rid of the second one and you should be fine. Also, calling res.end() after res.render() is redundant since res.render() automatically ends the response with the rendered result by default.

Just learned this! pass your responses through a function that checks if the response was already sent:

app.use(function(req,res,next){
  var _send = res.send;
  var sent = false;
  res.send = function(data){
    if(sent) return;
    _send.bind(res)(data);
    sent = true;
};
  next();
});

本文标签: javascriptCan39t set headers after they are sent on expressStack Overflow