Yeah, for anything that needs to deal with the result of the query (your console.log call, or anything else), you'll have to do that within the 'result' callback because the value won't be defined until then.

With node.js you have to think of everything on the top level happening right away, and the callbacks like 'result' or 'error' to happen at some unspecified time in the future. So, your console.log call is happening instantly, and the result callback is happening a few ms later. With node you just have to set up all the 'when X happens, call me back' stuff at the top level, and deal with results in their respective callbacks.

If you haven't, look into Promises and/or the async project, to make this all more manageable.

On 03/24/2015 11:21 AM, [email protected] wrote:
Hello
I am a beginner and struggling with the asynchronous nature of Node.js,
could you please give me a hint how to deal with following issue.

In my test code below, I do an SQL query. I will get callbacks for each result and I will then add counter value and store the result into an array. This all works fine because I am inside "this query process namespace" or whatever.

After the query is over, I will print out the array and the counter. They are both undefined! This I assume comes from the fact that inside my callbacks the process and environment was different. I have few questions: first one is, am I right with my assumptions that its not possible to get "stored" the parameter values somehow inside the callbacks to be used later outside the callbacks?

Should I try to handle and finish the whole operation inside the query callback, so that I dont even have to try to store these values outside this scope?? Any help and hints are very welcome :-)

function readDB() {
var counter = 0;
var testArray = new array();
var connection = mysql.createConnection({
host     : 'localhost',
user     : 'root',
password :  '',
database : 'TestDB'
});
connection.connect();
var query = connection.query('SELECT * from Users');

query.on('error', function(err) {
throw err;
});
query.on('fields', function(fields) {
});
query.on('result', function(row) {
counter += 1;
testArray[counter] = row;
console.log("testArray: " + testArray);
});

connection.end();
console.log("testArray: " + testArray + ", counter: " + counter);
}
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/e1156af2-d151-4607-837d-74f5dcc9ae5b%40googlegroups.com <https://groups.google.com/d/msgid/nodejs/e1156af2-d151-4607-837d-74f5dcc9ae5b%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/55119FC9.3090009%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to