Returning data from Axios API

前端 未结 8 1327
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 11:52

I am trying to use a Node.JS application to make and receive API requests. It does a get request to another server using Axios with data it receives from an API call it rece

8条回答
  •  独厮守ぢ
    2020-12-07 12:48

    axiosTest() is firing asynchronously and not being waited for.

    A then() function needs to be hooked up afterwards in order to capture the response variable (axiosTestData).

    See Promise for more info.

    See Async to level up.

    // Dummy Url.
    const url = 'https://jsonplaceholder.typicode.com/posts/1'
    
    // Axios Test.
    const axiosTest = axios.get
    
    // Axios Test Data.
    axiosTest(url).then(function(axiosTestResult) {
      console.log('response.JSON:', {
        message: 'Request received',
        data: axiosTestResult.data
      })
    })

提交回复
热议问题