0

the console.log in the showBets function is working fine, but not the res.write. Am i misunderstanding how it works? The documentation is not making me any wiser.

app.js

var queryString = 'SELECT * FROM bets';

router.get("/bets",function(req, res){
    showBets(res);
    res.sendFile(path + "bets.html");
});

function showBets(res){
    connection.query(queryString, function(err, rows, fields, res) {
        if (err) throw err;

        else{
            for (var i in rows) {
                res.write(rows[i].title);
                console.log(rows[i].creator);
            }
        }
    });
}
Simon B
  • 98
  • 10
  • you do `res.end();`, do you? – llamerr Mar 11 '16 at 11:46
  • No, should I? To close the query? – Simon B Mar 11 '16 at 12:06
  • I don't have any express setup right now, but this answer says that you should http://stackoverflow.com/a/21749726/319375 – llamerr Mar 11 '16 at 13:32
  • I tried adding res.end after the forloop, I'm getting write after end error, which is weird since I don't think I'm writing after the res.end? – Simon B Mar 11 '16 at 13:38
  • you also need to include changes from @DinhNC if you haven't yet. you need to close proper `res`. `res1` in his code is response from express `get`, `res2` is response from some sql driver `connection`. in your current code you trying to write into response from sql server – llamerr Mar 11 '16 at 13:45
  • and you also need to close it in correct place - after `res.sendFile`, not after for loop – llamerr Mar 11 '16 at 13:50
  • http://pastebin.com/p271LiyV This is my code at the moment, I'm still getting Error: write after end. – Simon B Mar 11 '16 at 14:11
  • try to add `console.log` before `res.end()` - http://pastebin.com/AZKcfHTU I guess what is going on is your callback `function(err, rows, fields, res2) {` is executed after `res.end();` is already done – llamerr Mar 11 '16 at 14:25
  • Yeah the console.log gets executed, do you have any clue what I can do to solve this? – Simon B Mar 11 '16 at 15:17
  • can you try to remove `res.end()`? maybe it's done automatically at the end of query (but how does it finds it?) and not needed after all? – llamerr Mar 11 '16 at 15:40
  • I get the same error even if i remove it, weird that such an easy thing gets so complicated – Simon B Mar 11 '16 at 15:51
  • hm. it's indeed must be very common and be done easily - some callback that isn't passed or something like this. but I can't think of anything right now. maybe I will setup some playground on weekend and try that – llamerr Mar 11 '16 at 15:59
  • I haven't yet got to it (and will not be able today), but have you looked at sendFile docs? http://expressjs.com/en/api.html#res.sendFile `The method invokes the callback function fn(err) when the transfer is complete or when an error occurs. If the callback function is specified and an error occurs, the callback function must explicitly handle the response process either by ending the request-response cycle, or by passing control to the next route.` Try to put their `res.status(err.status).end();` from example in your code accordingly – llamerr Mar 12 '16 at 06:39
  • Also maybe you need to send file first and only send your data afterwards, since sendfile may send it's own headers - in that case you will need to send your data inside that callback to sendFile – llamerr Mar 12 '16 at 06:45
  • Hey man thanks for the help I solved it by doing this: `code`function showBets(res) { var queryString = 'SELECT * FROM bets'; connection.query(queryString, [], function(err, rows) { if (err) { console.log("aa: " + err); throw err; } rows.forEach(function(row) { res.write(row.creator); console.log(row.creator); }); res.end(); }); } `code` – Simon B Mar 12 '16 at 16:27
  • http://pastebin.com/MXMe2LY6 – Simon B Mar 12 '16 at 16:28
  • glad to hear. btw you can add your solution as your own answer and accept it later – llamerr Mar 12 '16 at 16:41

2 Answers2

2

You should be careful about "res" variable

function showBets(res1){
    connection.query(queryString, function(err, rows, fields, res2) {
        if (err) throw err;

        else{
            for (var i in rows) {
                res1.write(rows[i].title);
                console.log(rows[i].creator);
            }
        }
    });

res1 and res2 are difference.

llamerr
  • 2,997
  • 4
  • 29
  • 40
DinhNguyen
  • 356
  • 3
  • 14
  • I get the error: Error: write after end now. Why is it I need to be careful about the res variable? – Simon B Mar 11 '16 at 12:10
0

Fixed it by doing this:

function showBets(res) {
  var queryString = 'SELECT * FROM bets';

  connection.query(queryString, [], function(err, rows) {
    if (err) {
      console.log("aa: " + err);
      throw err;
    } 

    rows.forEach(function(row) {
      res.write(row.creator);
      console.log(row.creator);
    });

    res.end();
  });
}
Simon B
  • 98
  • 10