-1

while using axios I have a post where I get a response but when I want to return the response to my other function it shows up as undefined any clues? on how I can fix it.

function PostRawData(rData) {
    console.log('test')
    axios.post("my api string blocked for privacy reasons", { rawData: rData })
      .then(function (response) {

        console.log(response.data.temperature);
        console.log(response.data)
        console.log('before return')
        return response.data 
      })
      .catch(function (error) {

        console.log(error);
      });
  }

  function checkBoundaries(rdata) {

    
    console.log(rdata)

    let itemss = []


      itemss = PostRawData(rdata);
      console.log('after')
      console.log(itemss)
      console.log(itemss.temperature)
      
      console.log(PostRawData(rdata))
      
      
      if (22.00 > 4.00) {
        console.log('if')
        return 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-blue.png'
      }
      else {
        console.log('else')
        return 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-red.png'
      }
      // ...
    }
Jaap Smit
  • 53
  • 8

1 Answers1

2

Try using async await. I like to write functions that return the axios request. A try/catch can handle the errors

async function PostRawData(rData) {
    return axios.post("my api string blocked for privacy reasons", { rawData: rData })
  }
async function checkBoundaries(rdata) {
    try {
    const response = await PostRawData(rData)
    const data = response.data
    //do whatever
    } catch (err) {
    console.error(err)
    }
}
Daniel M
  • 133
  • 3
  • 9