Search code examples
javascriptnode.jsapiaxioshttp-get

NodeJS Axios response undefined on console.log API


I try to make this code work.

const axios = require('axios');
let bodyapi = axios.get('there's my api')
console.log(bodyapi.data) <- undefined
let body = bodyapi.data
console.log(body.discord) <- couldn't get parameter ''discord'' of undefined

Response type of the API:

"discord":{"Category":"activation","Qty":1542,"Price":1}
"vkontakte":{"Category":"activation","Qty":133,"Price":21}

I get it ''undefined''. Running on NodeJS.


Solution

  • axios return promise

    const axios = require('axios');
    
    // use async await 
    (async ()=>{
    let bodyapi = await axios.get('there's my api')
    console.log(bodyapi.data) // response
    })()
    
    // other way
    axios.get('there's my api').then(data=> console.log(data))