I want to retrieve the unique id from the database and store them into the object (userslist
).
Why can not I retrieve data into an object?
Object userslist
is empty {}
I have this function in Node.js:
getOnlineUsers: function()
{
var userslist = {};
client.query(
'SELECT DISTINCT Aid FROM online',
function selectCb(err, results, fields) {
if (err) {
throw err;
}
if (results.length > 0) {
for (var i = 0; i < results.length; i++) {
var reader = results[i];
userslist[reader['Aid']] = {
fbid: reader['Aid']
}
}
}
}
);
return userslist;
}
Here's one example of what was discussed in the comments above.
Pass a callback to getOnlineUsers
...
my_obj.getOnlineUsers(function(u_list) {
/* do something with the user list */
});
And have getOnlineUsers
receive the callback argument, and invoke it...
// receive the callback----v
getOnlineUsers: function(cb_func) {
client.query(
'SELECT DISTINCT Aid FROM online',
function selectCb(err, results, fields) {
if (err) {
throw err;
}
var userslist = {}
if (results.length > 0) {
for (var i = 0; i < results.length; i++) {
var reader = results[i];
userslist[reader['Aid']] = {
fbid: reader['Aid']
}
}
}
cb_func(userslist); // <-- invoke the callback, passing the data
});
}
This is one approach, but basically illustrates that the continuation of the flow of code needs to take place in the callback passed to client.query
.