2

I have this code:

async = require('async')

async.auto({
   getUserName: function(callback) {
      console.log('**In getUserName.**')
      callback(null, 'Lem')
   },

   connectToDb: function(callback) {
      console.log('**In connectToDb.**')
      var connected = true
      if(connected) {
        callback(null, connected)
      } else {
        callback('Error connecting to DB.', null)
      }
   },

   checkIfUserExist: [
      'getUserName',
      'connectToDb',
      function(callback, results) {
         console.log('**In checkIfUserExist.**',          
            JSON.stringify(results))
         var userExist = false
         if(userExist) {
            callback('User exist in DB.')
         } else {
            setTimeout(
               function() {
                  callback(null, userExist);
               },
               1000
            );
         }
      }
   ],

   signup: [
      'checkIfUserExist',
      function(callback, results) {
         console.log('**In signup**', JSON.stringify(results))
         var userName = results.getUserName
         var isDbConnected = results.connectToDb
         var userExist = result.checkIfUserExist

        if(userName && isDbConnected && !userExist) {
           callback(null, 
             {'status': '200', 'msg': 'Successfully signed up user'})
       } else {
          callback('Error signing up user.', null)
       }
    }
  ]
 },
 function(error, results) {
    console.log('error = ', error)
    console.log('results = ', results)

})

Why am I experiencing this error:

**In getUserName.**
**In connectToDb.**
error =  function () {
    if (fn === null) throw new Error("Callback was already called.");
        var callFn = fn;
        fn = null;
    callFn.apply(this, arguments);
}
results =  undefined
**In checkIfUserExist.** undefined
^[[A/home/lem/js/async/asyncAuto.js:30
        callback(null, userExist);
        ^

TypeError: callback is not a function
   at Timeout._onTimeout (/home/lem/js/async/asyncAuto.js:30:13)
   at ontimeout (timers.js:365:14)
   at tryOnTimeout (timers.js:237:5)
   at Timer.listOnTimeout (timers.js:207:5)
1
  • in your instead of : signup: [ 'checkIfUserExist', function(callback, results) { use: signup: [ 'checkIfUserExist', function(err, results) { actually what node.js does is it first check the error in every function then it gives the result as a callback what you were doing is like you were passing callback as an error to which it gives you an error message Commented Oct 22, 2016 at 9:44

2 Answers 2

4

There was a breaking change in async's auto method in v2, which you seem to use. https://github.com/caolan/async/blob/master/CHANGELOG.md#breaking-changes

auto task functions now always take the callback as the last argument. If a task has dependencies, the results object will be passed as the first argument. To migrate old task functions, wrap them with _.flip

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

Comments

1

An async function, one that expects a callback as its last argument. Here is my working code.

    var async = require('async');
         async.auto({
           getUserName: function(callback) {
            console.log('**In getUserName.**')
            callback(null, 'Lem')
           },

         connectToDb: function(callback) {
          console.log('**In connectToDb.**')
          var connected = true
          if(connected) {
            callback(null, connected)
          } else {
            callback('Error connecting to DB.', null)
          }
       },
       checkIfUserExist: [
          'getUserName',
          'connectToDb',
          function(results, callback) {
             console.log('**In checkIfUserExist.**',          
                JSON.stringify(results))
             var userExist = false
             if(userExist) {
                callback('User exist in DB.')
             } else {               
                setTimeout(function() {                 
                      callback(null, userExist);
               },1000);
             }
          }
       ],

       signup: [
          'checkIfUserExist',
          function(results, callback) {
             console.log('**In signup**', JSON.stringify(results))
             var userName = results.getUserName
             var isDbConnected = results.connectToDb
             var userExist = results.checkIfUserExist

            if(userName && isDbConnected && !userExist) {
               callback(null, 
                 {'status': '200', 'msg': 'Successfully signed up user'})
           } else {
              callback('Error signing up user.', null)
           }
        }
     ],
},
     function(error, results) {
        console.log('error = ', error)
        console.log('results = ', results)         
    })

function(callback, results) should be function(results, callback)

Please check this url for reference. https://caolan.github.io/async/docs.html#auto

4 Comments

Still has errors see next comment below. What does that mean?
In getUserName. In connectToDb. error = function () { if (fn === null) throw new Error("Callback was already called."); var callFn = fn; fn = null; callFn.apply(this, arguments); } results = undefined In checkIfUserExist. {"getUserName":"Lem","connectToDb":true} In signup {"getUserName":"Lem","connectToDb":true,"checkIfUserExist":false}
It turns out to be a mistake in the orientation of the curly braces.
yes there was curly braces missed, I have update the code and now it's good.

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.