1

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?

Community
  • 1
  • 1
atkayla
  • 8,143
  • 17
  • 72
  • 132
  • Personally I'd use a promise library like [`Q`](https://github.com/kriskowal/q) and it's [`.all()`](https://github.com/kriskowal/q#combination) method – Bojangles Mar 25 '14 at 21:12

1 Answers1

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

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

insanoid
  • 413
  • 2
  • 10