Denoteone
Denoteone

Reputation: 4055

Check data being returned in a .then AngularJS

I am trying to figure out the best way to check what is being returned to my response in my .then{}

for example can I add a console.log to the .then{} to see what is being returned if anything?

Below is my unsuccessful attempt at checking this data:

return myValidationService.getUserDetails(userId)
                        .then(response => response.data.data
                          //This is where I want to add my log
                          console.log("This is my response data: " + response.data.data))
                        .catch(error => pageErrorService.go(pageErrorService.errorDetails.genericError, error));

I am getting complaints from my linter about the above syntax though.

What is the standard way of checking this data?

Upvotes: 0

Views: 46

Answers (2)

Rohit Goyal
Rohit Goyal

Reputation: 222

The issue is here in the syntax

return myValidationService.getUserDetails(userId)
     .then(response => response.data.data
       //This is where I want to add my log
       console.log("This is my response data: " + response.data.data))
     .catch(error => ....));

You are using fat arrow in the code:

 .then(response => response.data.data
      //This is where I want to add my log
       console.log(...)
  )

The above code is same as writing

.then(function(response){
   return response.data.data
})

The above code is returning the value, before it being consoled

Adding curly brackets will work in your case. You can write something like this to log the value:

 .then(response => {
      console.log(...);
      return response.data.data;
  })

Upvotes: 0

Varun Sukheja
Varun Sukheja

Reputation: 6526

In angular1.x it goes in this way:

.then(function(response){
  console.log(response.data);
})

Upvotes: 2

Related Questions