如何将 axios 响应保存到变量,以便在 axios 之外使用? [英] How do I save the axios response to variable, to be used outside axios?

查看:358
本文介绍了如何将 axios 响应保存到变量,以便在 axios 之外使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个聊天机器人,它从聊天中获取一条消息,并将其发送到一个 api,该 api 会根据该消息生成一个随机的 gif.

I'm building a chat bot, which takes a message from chat, and sends it to an api which generates a random gif based on the message.

axios 请求/响应工作正常,但我需要它将响应中的 URL 值存储为外部变量.

The axios request/response works correctly, but I need it to store a URL value from the response as an external variable.

变量/URL 然后将使用另一个函数在聊天中发送.此函数无法在 axios request/then 语句中运行,而且我似乎也无法从内部获取要更新的变量.

The variable/URL will then be sent in chat using another function. This function cant be run inside the axios request/then statement, and I cant seem to get the variable to update from inside either.

我对此有点困惑,还没有找到一种方法可以满足我的需要.任何建议表示赞赏.

I'm a bit stuck on this, and have yet to find a method which will do what I need it to. Any advice is appreciated.

如何从异步返回响应调用? 没有解决我如何从外部获取它,因为我再次无法从 then 语句内部运行消息函数.

How do I return the response from an asynchronous call? doesn't address how I can get this externally, as again I can't run the message function from inside the then statement.

const sentence = context.activity.text.replace('!guggy ', '');

//theoretically this would hold the api response, but this wont update as intended
//var apiResponse;

//api call function
   function postRequest(x) {
    return axios.post('http://exampleurl.here.com/helpapi', {
        "sentence" : x,
        "lang": "ru"
    },{
        headers: {
            "Content-Type":"application/json",
            "apiKey":"example-key-2020202"
                        }
                });
        }

//initiates api call, then logs response or error
postRequest(sentence).then(function(response) {                  
                   console.log(response.data.animated[0].gif.original.url); //this works, returns URL

//attempting to store the same value as external variable doesnt work
apiResponse = response.data.animated[0].gif.original.url; //the variable remains blank when called later

}).catch (function (error) {
    console.log(error);
});


//this has to be run outside of axios/then statement, or it will not work.
//this should take the url passed from the response, and send it as a chat message                     
await context.sendActivity(apiResponse);

这实际上不会返回任何错误,而是在服务器上运行.它只是在我需要时不返回变量 - 它是空白的.我假设这意味着我只是忽略或误解了一些重要的事情.

This doesnt actually return any errors, and runs on the server. It just doesn't return the variable when I need it - its blank. I'm assuming this means I'm just overlooking or misunderstanding something crucial.

推荐答案

代码片段中的执行顺序为:

The order of execution in the code snippet is:

  1. 调用 axios
  2. axios 通过网络发送消息.
  3. context.sendActivity() 使用空白 apiResponse 调用.
  4. exampleurl.here.com 返回 axios 的答案.
  5. axios 调用then"子句.
  6. apiResponse 被分配.
  1. Call axios
  2. axios sends the message over the network.
  3. context.sendActivity() is called with a blank apiResponse.
  4. exampleurl.here.com returns an answer to axios.
  5. axios calls the "then" clause.
  6. apiResponse gets assigned.

需要正确等待服务器返回错误,如下:

you need to properly wait for the server to return an error, as follows:

const sentence = context.activity.text.replace('!guggy ', '');

async function postRequest(x) {
    return await axios.post('http://exampleurl.here.com/helpapi', {
            "sentence" : x,
            "lang": "ru"
            },{
                headers: {
                    "Content-Type":"application/json",
                    "apiKey":"example-key-2020202"
                }
            });
}

try {
    const response = await postRequest(sentence);
} catch (error) {
    console.log(error);
}

console.log(response.data.animated[0].gif.original.url);
apiResponse = response.data.animated[0].gif.original.url;
await context.sendActivity(apiResponse);

这篇关于如何将 axios 响应保存到变量,以便在 axios 之外使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆