Search code examples
javascriptreactjsredux-saga

ReactJS getting results from a Promise


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


Solution

  •   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)