Search code examples
angularjsrestangular

Not returning Restangular back-end call correctly


My back-end call is returning undefined. A.k.a TypeError: Cannot read property 'then' of undefined. I think I am calling it incorrectly.

Here is the AngularJS controller code:

$scope.addUser = function (chaseUser) {
    Accounts.addChaseUser(userToSubmit).then(function (response, err) {
        if (err) {
            $scope.errorMessage = "There was an error.";
            $log.debug(err);
        } else if (response) {
            $scope.errorMessage = "It worked.";
            $log.debug(response);
        } else {
            $scope.errorMessage = "No 'response' nor 'err' returned from backend";
        }
    });
};

How this responds is that if...

(1) I put in correct credentials, I get a response that comes back with all the transaction data but still TypeError: Cannot read property 'then' of undefined in the console.

(2) Input incorrect credentials, I get no error object, response object, or even making it down to the line where I have $scope.errorMessage = "No 'response' nor 'err' returned from backend"; plus, of course, `cannot read property 'then' of undefined.

Corresponding AngularJS service:

return {
    addChaseUser: function(credentials) {
        return Restangular.one('user').customPOST(credentials, 'addUser');
    }
};

On the backend (controller):

module.exports = {

   addChaseUser: function (req, res) {
      PlaidService.provideCredentialsToMFA(req.body, function (err, mfaRes) {
        if (err) {
          return res.status(403).json(err);
        }
        return res.json(mfaRes);
      });
    },

 };

Backend service:

var plaid = require('plaid');
var plaidClient = new plaid.Client('test_id', 'test_secret', plaid.environments.tartan); 

module.exports = {
  provideCredentialsToMFA: function (credentials, cb) {
    Q.fcall(PlaidService.connectUser.bind(this, credentials))
      .then(PlaidService.saveUsersAccessToken.bind(this, credentials))
      .then(PlaidService.getTransactionData.bind(this, credentials))
      .then(function(transactions) {
        cb(null, transactions);
      },
      function(err) {
        console.log(err);
        cb(err, null);
      });
  },

}

How am I supposed to be calling this Restangular POST from the AngularJS controller? It should not be returning undefined.


Solution

  • Since you are getting the response from the server side, it means that your server side code and the angular service code is working just fine. :)

    The only possibility I can see is that I is wrong with the application of .then() block is that insted of two parameters(i.e., response and err) lets try with only one parameter.

    Something like following :

        $scope.addUser = function (chaseUser) {
            Accounts.addChaseUser(userToSubmit).then(function (response) {
                if(response.status == 'error'){
                   $log.debug('Got error in the response');
                }else{
                   $log.debug('SUCCESS');
                }
            });
        };
    

    I have used .then(function(data)) with only one parameter. That is the only thing I could find :)