Tuesday 14 March 2017

NodeJs StrongLoop nested callbacks not working properly

I have written a custom endpoint API in LoopBack by StrongLoop, but it doesn't work properly. At first it works fine but on second or third iteration it doesn't work e.g If I post word "API" it works, then if I search "eat" it works also but when I post word "API" again it doesn't work i.e browser goes into loop. Im using nested callbacks. Here is my code

getTags(word,function(response){
     if(response == true){
        res.send(array); //send back response to browser
     }
});


function getTags(word,callback){
     getPosts(word,function(response){
         callback(response);
     })
}

function getPosts(word,callback){
     getUser(word,function(response){
         callback(response);
     })
}

function getUser(word,callback){
     db.find(function(err,success){
        if(success){
           callback(true);
        } else {
           callback(false);
        }
     }) //User find logic.

}

Kindly help me on this issue. Is this a callback hell problem? If so, how can I resolve it?



via Shahrukh Shahid

No comments:

Post a Comment