There is no place where i can find the answer i am looking for 8 weeks to this stupid answer, please help me!!!
Well i am creating a for loop to get a topic list for my forum from the database server:
for(i=0; i<data.length; i++){
var ownerid = data[i].ownerid,
info = require(__dirname + '/info.js').run(ownerid, userDataHost, userDataUsername, userDataPassword, userDataName);
console.log('1)' + info);
responseOb['latestTopics'] = responseOb['latestTopics'] + '<li><a href="/topic/' + data[i].id +'">' + data[i].name + '</a><span>By ' + info.username + '</li>';
}
But as you can see there is 1 require wich need to return a object wich i use in de other responseObject, well the problem is that in use a query in that script and the for loop doesn't wait till he gots a return so info.username is undifined????????
info.js:
exports.run = function(userid, dataHost, dataUsername, dataPassword, dataName) {
var info = {username: 'Err'},
testSQL = require('mysql').createConnection({host: dataHost, user: dataUsername, password: dataPassword, database: dataName}),
useridEscaped = testSQL.escape(userid);
testSQL.query('SELECT * FROM `users` WHERE `id`=' + useridEscaped, function(err, data) {
if (!err){
if (data.length > 0){
info['username'] = data[0].username;
sendBack();
} else {
info['username'] = 'undifined';
sendBack();
}
} else {
sendBack();
}
});
function sendBack(){
console.log(info);
return info;
}
}
Please help me!!!!!!!!