1

I'm trying to code a function that does two queries and I wish to have access to both record sets in the final code-block; my code looks similar to:

295     connection.query(sql1, function selectDb(err, results, fields) {
296         if (err) {
297           throw err;
298         }
299
300         result = results[0];
301         console.log(result);
302
303         console.log(sql2);
304         connection.query(sql2, function selectDb(err, results, fields) {
305             if (err) {
306               throw err;
307             }
309         console.log(result);

problem is the var result retains the first record set until after the second query @304 and displays 'undefined' @309 (had the record set in previous displays).

I don't know how to pass the first record set value to the inner code async code block @304; Tried to add result as a parameter;

304         connection.query(sql2, function selectDb(err, results, fields, result) {

result still undefined @309

Help appreciated.

1
  • Could you provide more detail? what db library are you using? What is the result you are expecting from the db? What is actually returned? etc… Also - missing a bit of code in the sample (no closing brace) Commented Aug 16, 2013 at 1:53

2 Answers 2

3
  1. Callback argument names are completely your choice. There is a spec that tells you what will be passed into which argument and the slots may be named to aid further description, but the actual name of the actual argument it is passed into is up to you. Keeping the expected name is a good idea for others reading the code in the future, but can be varied where expedient. This means you can use results1 and results2 instead of only results, in the callbacks, if it would help.
  2. Your second callback is a closure and can read the variables in the block where it was defined.
  3. I've heard it is bad practice to name your anonymous function callbacks.
???    // variables sql1, sql2 need to be defined prior to the code below
295    connection.query(sql1, function(err, results1, fields1) {
295.1       var result;  // dont make result a global
296         if (err) {
297           throw err;
298         }
299
300         result = results1[0];
301         console.log(result);
302
303         console.log(sql2);
304         connection.query(sql2, function(err, results2, fields2) {
305             if (err) {
306               throw err;
307             }
309             console.log(result);
310             console.log(results1);
311             console.log(results2);
312       });
313   });
Sign up to request clarification or add additional context in comments.

Comments

0

changed the name of the variable from result to app; works now; must be something magic about the label 'result'; using mysql.Connection.

1 Comment

There is nothing magic about the name result at all - it's just that the second result argument shadowed the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.