Saturday 22 April 2017

Node crashes - TypeError: callback is not a function

I have the following code. Whenever the code reaches that it needs to return a callback I get the following error:

TypeError: callback is not a function

This is my code:

exports.myCoolOperation = function(myParam, callback) {

  var mod_datastore = require('@google-cloud/datastore')({
    projectId: params.GAE_PROJECTID,
    keyFilename: params.GAE_KEYFILE
  });

  var myLibrary = include('lib/myLibrary');

  myLibrary.doSomething(myParam, function(err, result) {
    if (err) {
      //Server crashes here with TypeError: callback is not a function      
      return callback('error');
    } else {
      if (result) {

        var transaction = mod_datastore.transaction();

        transaction.run(function(err) {
          if (err) {
            //Server crashes here with TypeError: callback is not a function  
            return callback('error');
          }

          transaction.save([{
            key: mod_datastore.key(['myTable', myParam]),
            method: 'insert',
            data: {
                name: 'date',
                value: 'Todays date'
              });

          transaction.commit(function(err, apiResponse) {
            if (!err) {
             //Server crashes here with TypeError: callback is not a function
              return callback(null, 'Everything is cool');
            }

            //Server crashes here with TypeError: callback is not a function
            return callback('error');
          });


        });

      } else {
        return callback('error');
      }
    }

  });
}

I am calling this module like so:

myModule.myCoolOperation(req.body.myparam, function (err, result) {
        if (err) {
            return next(err);
        } else {
            return res.status(200).json({res: result});
        }
    });



via user2924127

No comments:

Post a Comment