0

I try to call $http service within functions so I don't get nested callbacks. Without function scope it look like this:

      $http.post('/verify_stripe_token', {email:$routeParams.email,token:$routeParams.t})
      .then(function(response) {
        console.log(response.data) // return 1
      });

I want to use if(valid_token() == 1){} so I tried

function valid_token(){
      $http.post('/verify_stripe_token', {email:$routeParams.email,token:$routeParams.t})
      .then(function(response) {
        return response.data;
      });
    }

Nothing worked? I do console.log(valid_token()) I got undefined.

Jenny Mok
  • 2,744
  • 9
  • 27
  • 58

2 Answers2

2

First of all your valid_token() function doesn't return anything.

Next, you can't do if(valid_token() == 1) since $http requests are asynchronous

You would need to do something like:

function valid_token(){
  // return the promise
  return $http.post('/verify_stripe_token', {email:$routeParams.email,token:$routeParams.t})
  .then(function(response) {
    return response.data;
  });
}

valid_token().then(function(data){
  if(data == 1){
     // do what you need here
  }
})
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Response is actually a JSON.Print response.data to confirm it and handle it the JSON way

Jonathon
  • 49
  • 1
  • 12