atkayla
atkayla

Reputation: 8859

nodejs (expressjs) how to pass information from 2 queries into jade?

I have a table of users and their information and a different table to keep track of friends: setting up a friend list in mysql

I am trying to do something like this using jade, profile.jade:

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

    - each friend in friends
        p=row.friend2

express

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

^ What is the proper way to do this?

Upvotes: 1

Views: 165

Answers (1)

insanoid
insanoid

Reputation: 413

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

This is a simple solution which would make it work, however makes the queries run sequentially hence increasing loading time.

Upvotes: 1

Related Questions