javascript - Node.js callbacks hell -


i'm learning node.js right , i'm having troubles calling back.

i looked @ event emitter doesn't seems relevant me.

this i'm calling:

exports.search = function(lat, lng, arr, callback) {      //something      geocoder.reverse({         lat: lat,         lon: lng     }, function(err, res, callback) {          //finding area         if (area !== "null") {             pool.getconnection(function(err, connection, callback) {                 if (err) {                  } else {                     connection.query("some sql code", function(err, rows, fields, callback) {                         if (found im looking                             for) {                             connection.query("some sql code", function(err, rows, fields, callback) { //looking else                                 if (err) {                                     callback(true);                                 } else {                                     if (rows[0] === undefined) {                                         callback(true);                                     } else {                                         console.log("found!");                                         callback(null, rows[0]);                                     }                                 }                             });                          } else if (err) {                          }                     });                 }             });         } else {          }     }); }; 

i'm getting "found!" in console log, callback doesn't working reason.

if put callback @ end of function " search " call back, know i'm not having problem function gets callback.

thank you!

i think callback(null, rows[0]) call of function geocoder.reverse. can change name callback of search function callback1 , call below.

exports.search = function(lat, lng, arr, callback1) {  //something  geocoder.reverse({     lat: lat,     lon: lng }, function(err, res, callback) {      //finding area     if (area !== "null") {         pool.getconnection(function(err, connection, callback) {             if (err) {              } else {                 connection.query("some sql code", function(err, rows, fields, callback) {                     if (found im looking                         for) {                         connection.query("some sql code", function(err, rows, fields, callback) { //looking else                             if (err) {                                 callback(true);                             } else {                                 if (rows[0] === undefined) {                                     callback(true);                                 } else {                                     console.log("found!");                                     callback1(null, rows[0]);                                 }                             }                         });                      } else if (err) {                      }                 });             }         });     } else {      } }); 

};

you can apply async lib code code clear too.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -