【问题标题】:nodejs (expressjs) how to pass information from 2 queries into jade?nodejs(expressjs)如何将来自2个查询的信息传递给jade?
【发布时间】:2014-05-03 23:42:33
【问题描述】:

我有一张用户表和他们的信息,还有一张不同的表来跟踪朋友:setting up a friend list in mysql

我正在尝试使用翡翠,profile.jade 做这样的事情:

    - each user in users
        h1='Welcome to '+user.username+' profile!'

    - each friend in friends
        p=row.friend2

表达

function select(id, res) {
    connection.query(' SELECT * FROM profiles WHERE username = "'+id+'" ' , function(err, rows){
        res.render('profile', {users : rows});  
    });

    connection.query(' SELECT * FROM friends WHERE friend1 = "'+id+'" ' , function(err, rows){
        res.render('profile', {friends: rows});     
    });
}

^ 这样做的正确方法是什么?

【问题讨论】:

  • 我个人会使用像Q 这样的promise 库,它是.all() 方法

标签: javascript mysql node.js express pug


【解决方案1】:
connection.query(' SELECT * FROM profiles WHERE username = "'+id+'" ' , function(ferr, profile_rows){
 connection.query(' SELECT * FROM friends WHERE friend1 = "'+id+'" ' , function(err, friends_rows){
  res.render('profile', {friends: friends_rows, users : profile});     
 })
});

这是一个简单的解决方案,可以使其正常工作,但会使查询按顺序运行,从而增加加载时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多