简体   繁体   中英

Access JSON response.status outside the function (Axios)

Im using Facebook API, trying to create a custom

async function createAudience(data) {
  try {
    console.log(` Upload: ${JSON.stringify(data)}`);
    return await axios.post(`https://example.com/{account_id}/audience`, data);
  } catch (error) {
    console.error(error);
  }
}

And it works just fine, but I need to access the response.status outside the function, and im lost...

      const ca_response = await createAudience(audienceData)
  .then((result) => {
    return result.status })
  .catch(err => console.error(err));

I can push it and it works, I can acces ((result) => { return result.status }) from the function without an issue, but can't work with it from ouside... How can i create the const audienceStatus to be used on the rest of my code?? Thanks!

Just create a variable outside of your function and store the result.status inside of it instead of returning it, like myVar = result.status

you can do this:

const ca_response = await createAudience(audienceData);
console.log('audienceStatus', ca_response.status );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM