Search code examples
angularjsrestrestangular

Get result of rest request instead of response?


I have the following function in my controller.

RestRequestsSrvc.getUserDetail()
  .then(
    function (response) {
      $scope.user.userDetail = response;
    },
    function (error) {
      // TODO
    });

If I type

console.log(RestRequestsSrvc.getUserDetail());

the console logs a promise. I want to set a variable the the response. How can I modify my code so that I get the response instead of a promise?


Solution

  • Return a promise because your request is async.

    You should wait the response,

    Putting the console.log inside the callback function should print your info.

    RestRequestsSrvc.getUserDetail()
      .then(
        function (response) {
          $scope.user.userDetail = response;
          console.log(response);
        },
        function (error) {
          // TODO
        });