简体   繁体   English

res.write不起作用,而console.log中也起作用

[英]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. showBets函数中的console.log可以正常工作,但res.write不能正常工作。 Am i misunderstanding how it works? 我误会了它的工作原理吗? The documentation is not making me any wiser. 该文档并没有使我变得更明智。

app.js 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);
            }
        }
    });
}

You should be careful about "res" variable 您应该注意“ res”变量

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. res1和res2是不同的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM