Home > Enterprise >  ReactJS getting results from a Promise
ReactJS getting results from a Promise

Time:11-18

In my sagas, I am sending user credentials to the backend

const request = client.post('api/JwtAuth/GenerateToken', {UserName: action.username, Password: action.password})
console.log(request) 

client looks like this

import axios from "axios"

const client = axios.create({
    baseURL: 'https://localhost:5001/',
    timeout: 600000000000,
    headers: {
        ContentType: 'application/json'
    }
})

export default client

Chrome Browser console

CodePudding user response:

what is your question? If the question why are you seeing Promise on the console? That's because you are dealing with promises, either use .then

client.post().then(res => console.log(res.data)).catch(err => console.log(err))

or async await syntax

await client.post()

CodePudding user response:

  const request = client.post('api/JwtAuth/GenerateToken', {UserName: 
    action.username, Password: action.password})

  request.then(response => {
    // the response body can be accessed through the "data" property
    console.log(response.data)
  })
  console.log(request) 

UPDATE:

You are doing this inside a saga, right? If so, the best way to get the request response data is to use the call effect. Import it from redux-saga/effects, and get the response like this:

  const response = yield call(
    client.post,
    'api/JwtAuth/GenerateToken',
    { UserName: action.username, Password: action.password }
  )

  console.log(response.data)
  • Related