0

I want to save in localStorage my token from server(server it's made in java) but the code below doesn't work so I want to print res too see what data do I get from server.

I tried to print to console with res.data.text() and res.data.json() but doesn't work.

    return dispatch =>{
        return api.user.login(userData).then(res =>{
             const token = res.data;
             console.log(token);
             //localStorage.setItem('jwtToken',token);

        })
}
import axios from 'axios'

export default {
    user:{
        login:(credentials) => axios.post('http://localhost:8080/api/auth/signin',credentials).then(res=>res.data.user)
    }
}

The post request works on the server, but it gets stuck when I want to print.By stuck I mean when I press the login button nothing happens.

How can I print into the console res?

Robert Carlos
  • 31
  • 1
  • 7

3 Answers3

1

You could try to convert json object to string to see on console

return dispatch =>{
        return api.user.login(userData).then(res =>{
             const token = res.data;
             console.log(JSON.Stringify(res));
             //localStorage.setItem('jwtToken',token);

        })
}
Abdulla Thanseeh
  • 9,438
  • 3
  • 11
  • 17
1
axios.post('http://localhost:8080/api/auth/signin',credentials)
    .then(
        res=>console.log(res.data.user)
    )

Try this. I have written a console.log() inside the .then function. If it still doesn't work, try logging res instead of res.data.user and check the result

Anitta Paul
  • 455
  • 4
  • 15
0

For a successful request:

  return dispatch => api.user.login(userData)
      .then(res => console.log(res.data))

For a unsuccessful request:

Logging the error request you'll print the error object.

    return dispatch => api.user.login(userData)
      .then(res => console.log(res.data))
      .catch(error => console.log(error.request)) 

To store the token:

localStorage.setItem('jwtToken', JSON.stringfy(token))
Cyro
  • 609
  • 5
  • 11