从MySQL数据库获取数据并将其传递给jade,然后使用nodejs访问它

从MySQL数据库获取数据并将其传递给jade,然后使用nodejs访问它,mysql,node.js,pug,Mysql,Node.js,Pug,我已经开始使用nodejs在其中制作一个简单的聊天应用程序。 为此,我想访问mysql数据库中的所有用户 我的代码如下: JS 那么,如何访问yourview.jade中的数据呢 我还想知道我们应该为哪种类型的应用程序使用nodejs 先谢谢你 这是因为节点是异步的。您的res.sendstr在调用内部connection.query后立即执行,因此在发送之前没有执行添加更多内容的回调。 不,您的str变量不是全局变量。 我不这样认为。如果要在发送响应之前写入响应,可以使用res.write。

我已经开始使用nodejs在其中制作一个简单的聊天应用程序。 为此,我想访问mysql数据库中的所有用户

我的代码如下:

JS

那么,如何访问yourview.jade中的数据呢

我还想知道我们应该为哪种类型的应用程序使用nodejs

先谢谢你

这是因为节点是异步的。您的res.sendstr在调用内部connection.query后立即执行,因此在发送之前没有执行添加更多内容的回调。 不,您的str变量不是全局变量。 我不这样认为。如果要在发送响应之前写入响应,可以使用res.write。 是的,你可以用翡翠来代替你现在所做的。请看下面的图片。它们充满了jade中使用的变量示例。不过,为了给您总结一下,您应该能够使用{variable}访问变量

至于你的最后一个问题,我真的不知道你在问什么。您可以使用node处理许多内容,从到

exports.authenticate = function(req, res) {

    //connection.connect();
    var sql="SELECT * from users where username='"+req.body.user+"' and password='"+req.body.pass+"' LIMIT 1";
    connection.query(sql, function(err, rows, fields) {
        if (err) throw err;

        //res.send('Your data is: ', rows);
        var str="Hi, <b>"+rows[0].name+"</b> ("+rows[0].email+")";

        sql="SELECT DISTINCT username,name from users ORDER BY name";
        connection.query(sql, function(err, datarows, fields) {
            if (err) throw err;
            //res.send('Your data is: ', rows+' <br/> All Users are : ', datarows.length+"<a href='/'>Login</a>");
            str+='<ul style="list-style:none;width:300px">';
            for(var index=0;index<datarows.length;index++)
            {
                str+="<li><a href='javascript:;'>"+datarows[index].name+", "+datarows[index].email+"</a></li>";
            }
            str+='</ul>';console.log(str);  
            console.log(str)//gives ul  
        });

        str+="<a href='/'>Login</a>";
        res.send(str);//this not gives the ul of other users
    });

}
app.get('/', function(req, res) {
  mgs(dbColl).find(function(data){
    res.render('yourview.jade', { data: data });
  });
});