0

I'm writing a basic web app in Node, Express and MySQL. Its just a basic site, where a user can create an account, view other peoples accounts, and post infomation on their profile, and I'm creating it to learn Node and javascript. So, I'm up to creating the userlist, and I've created a nice function that returns all the users in the database. Here is the code:

function getAllUsers() {
    var query = " \
    SELECT \
      id, username, email, password \
    FROM \
      users \
    ";
    connection.query(query, function(err, rows) {
        if (err) throw err;
        return rows;
    });
}

Now, I'm using this function here:

router.get('/list', function(req, res) {
    var users;
    users = getAllUsers();
    res.render('user/list', {'title': 'Users', 'users': users});
});

But users is undefined. Why is this?

I'm pretty sure the MySQL connection is working properly, but here is how I connect it, just in case it comes in handy:

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'firstnodeapp'
});

connection.connect();

Thanks

--- EDIT --- I thought I should also mention, rows is defined in the getAllUsers() function, but once I return it it is no longer defined.

IHazza
  • 23
  • 6
  • 1
    `getAllUsers` is async, how to return value ? [Try this one](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?noredirect=1&lq=1) – The Reason Nov 16 '16 at 23:01
  • Hang on... I don't believe this should be marked as duplicate. My question wasn't "how do I return from an asynchronous function"... although the answer was related, the question was not... – IHazza Nov 25 '16 at 23:09

2 Answers2

1

Callbacks to the rescue (the most simple answer)

Your query is executing asynchronous, that means, when the function ends the query is still executing and your "users" variable ends being undefined.

Just do this:

function getAllUsers(callback) {
    var query = " \
    SELECT \
      id, username, email, password \
    FROM \
      users \
    ";
    connection.query(query, callback);
}

router.get('/list', function(req, res) {
    getAllUsers(function(err, rows) {
          // Or better yet, respond with the correct html code
          if (err) throw err;

          res.render('user/list', {'title': 'Users', 'users': rows});
    });
});

It should work pretty well. Study about Callbacks/Promises/Observables

LuisKx
  • 312
  • 1
  • 3
  • 7
0

Didn't you forget to return?

function getAllUsers() {
  ...

  var query_result = connection.query(query, function(err, rows) {
    if (err) throw err;
    return rows;
  });

  return query_result;
}
medik
  • 1,174
  • 12
  • 17
  • aha... I was returning from the query function rather than the getAllUsers() function... but this code complains about rows not being defined? – IHazza Nov 16 '16 at 22:57
  • I edited my answer, please try now. – medik Nov 16 '16 at 23:03
  • Thankyou, worked perfectly :) I'm surprised I didn't notice this myself! – IHazza Nov 16 '16 at 23:14
  • Ok, turns out I can't get the rows from the query object, so I think I'm going to go with LuisKx's answer, but thanks, this cleared a lot up for me :) – IHazza Nov 16 '16 at 23:22