Simon B
Simon B

Reputation: 98

res.write doesn't work while the same thing works in console.log

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);
            }
        }
    });
}

Upvotes: 0

Views: 409

Answers (2)

Simon B
Simon B

Reputation: 98

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();
  });
}

Upvotes: 0

DinhNguyen
DinhNguyen

Reputation: 356

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.

Upvotes: 2

Related Questions