2

I am trying to use a callback to indicate when all the async workers are complete, but I am getting the dreaded

TypeError: callback is not a function.

I would like to individually process each element in data, and on completion, have queue.drain to send the callback(data) to refresh Data on completion. I have been readying the async documentation, but clearly i am not getting something.

function refreshData(postData, callback) {

    var options = {
        host: 'www.myhost.com',
        port: 443,
        path: '/pulldata,
        method: 'POST',
        headers: {
            "Content-Type": "application/json"
        }
    };

    var req = https.request(options, function(res) {
       var headers = res.headers
       var d = '';
       res.setEncoding('utf8');
       res.on('data', function (chunk) {
           d = d + chunk;
       });
       res.on('end', function() {
           if (res.statusCode == '200') {
               data = JSON.parse(d);

                queue = async.queue(function (task, cb) {
                    processData(task,cb);
                 },1); 

                 //this is were the errors are
                 queue.drain = function() {
                    callback(data)
                  };

                 for(i=0; i<data.length; i++) {
                     queue.push(data[i],'');
                  }

            } else {
                 callback(false)
            }
        }); 

    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    // write data to request body
    req.write(postData);
    req.end();  
}   

Any assistance would be greatly appreciated!

Edit, added some pseudo code to demonstrate how refreshData is being used:

Node https.createServer(req,res) {
    req.on(){
       read userData
    }
    req.end(){
        validateUser(userData, function(callbackData) {
            if(callbackData==false) {
                //bad user or error with request
                res.writeHead(404);
                res.end('bye');
             } else {
                //good user and responses
                res.writeHead(200);
                res.end(callbackData);
             }
        })
    }
}

function validateUser(userData,callback) {
     //do some stuff to validate
     if(userData is good) {
     //call refreshData if user
          refreshData(userData,callback)
     } else {
          callback(false)
     }
}
3
  • 1
    how are you calling refreshData - clearly without a second argument that is a function Commented Apr 11, 2017 at 3:53
  • thanks, yes refreshData is a function that is being called on completion of a separate GET request. Commented Apr 11, 2017 at 10:55
  • edited post to add pseudo code to demostrate Commented Apr 11, 2017 at 11:33

1 Answer 1

3

[EDIT] Added a callback As given in the documentation you pointed to , change this line

queue.push(data[i],'');

to

queue.push(data[i], function(err){
 // handle error 
});

Try it here async-queue-callback

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but not sure that fixes my problem. In your example there is no callback on queue.drain, which doesn't solve my specific problem.
Added a callback. Please check the link again.
Can you please upvote and accept the answer. Others will know this is solved.

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.