-1

receiving an empty array, outside of an function ?

var triggerImageQuery = function(start, length,callback) {
  var feedData = [];
  var feedInfo = result.rows[start];
  var imgQuery = pgFormat("select * from feedImages where feedId=%L",feedInfo.feedid);
    model.client.query(imgQuery,function(err,result){
        if(result.rows.length > 0){
            var imgArr =[];
            for(var j=0;j<result.rows.length;j++){
                    var image = "http://"+config.host+":"+config.port+"/"+result.rows[j].imageurl;
                        imgArr.push(image);
            }
            feedData.push(feedInfo);
            feedData.push(imgArr);
        }
        else{
            feedData.push(feedInfo);
        }
console.log(feedData) // prints data correctly
    });
console.log(feedData) // here data gets empty?
    if(start < length) {
        start++;
        triggerImageQuery(start, length-1);
    }
    callback(feedData); // unable to callback here because of empty array`
}
triggerImageQuery(0, result.rows.length,function(result){
    res.json(result); // `result is empty`
});

even i have tried with declaring the var feedData = []; at the top, but no use.

and also tried the callback inside the model.client.query but there is an error like TypeError: callback is not an function.

karthik
  • 305
  • 1
  • 3
  • 14
  • 3
    Your `model.client.query` call is async - so your empty log executes before the query has completed. You need to do all the work in the callback from that call. – tymeJV Mar 14 '16 at 13:34
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Joe Clay Mar 14 '16 at 13:53
  • and even i have tested the callback(feedData) inside the model.client.query, it is throwing the error like TypeError: callback is not a function – karthik Mar 14 '16 at 14:19

1 Answers1

0

Just need to return data inside client.query method.

var triggerImageQuery = function (start, length, callback) {
    var feedData = [];
    var feedInfo = result.rows[start];
    var imgQuery = pgFormat("select * from feedImages where feedId=%L", feedInfo.feedid);
    model.client.query(imgQuery, function (err, result) {
        if (result.rows.length > 0) {
            var imgArr = [];
            for (var j = 0; j < result.rows.length; j++) {
                var image = "http://" + config.host + ":" + config.port + "/" + result.rows[j].imageurl;
                imgArr.push(image);
            }
            feedData.push(feedInfo);
            feedData.push(imgArr);
        }
        else {
            feedData.push(feedInfo);
        }
        console.log(feedData) // here data gets empty?
        if (start < length) {
            start++;
            triggerImageQuery(start, length - 1);
        }
        callback(feedData);
    });
}
triggerImageQuery(0, result.rows.length, function (result) {
    res.json(result); // `result is empty`
});
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95