Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

javascript - 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!

question from:https://stackoverflow.com/questions/65945702/access-json-response-status-outside-the-function-axios

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...