简体   繁体   English

如何将我从 JavaScript 中的提取请求中获得的数据保存到我的 function 外部的变量中

[英]How do I save the data I get from a fetch request in JavaScript into a variable that is external to my function

Obvsiouly I'm very new to Javascript.显然我对 Javascript 很陌生。 For an app that I'm building, I need to save the data I get from a fetch request into a variable that ideally I can access outside of my function.对于我正在构建的应用程序,我需要将从获取请求中获得的数据保存到一个变量中,理想情况下我可以在 function 之外访问该变量。 Any light on this would be hugely appreciated.对此的任何启示将不胜感激。

Here's my code:这是我的代码:

function generateNewQuote() {
    fetch('https://api.whatdoestrumpthink.com/api/v1/quotes/random')
           .then(response => response.json())
           .then(data => {
            const newMes = data.message;
            console.log(newMes );
        });
}


I need to get the value of my newMes variable into a variable that I can specify in the global scope or alternative a way to access the newMes variable locate inside my generateNewQuote function in the Global Scope.我需要将我的 newMes 变量的值转换为我可以在全局 scope 中指定的变量,或者访问位于全局 Z5D113F2038D289F29E861 中的 generateNewQuote function 中的 newMes 变量的方法。

Change the variable Scope.更改变量 Scope。

let newMes = [];
function generateNewQuote() {
    newMes = [];
    fetch('https://api.whatdoestrumpthink.com/api/v1/quotes/random')
           .then(response => response.json())
           .then(data => {
            newMes = data;
            console.log(newMes.message );
        });
}

This may not be the most right answer, but this works.这可能不是最正确的答案,但这是可行的。 As setTimeout also works somewhat like await outside an async function.因为 setTimeout 也有点像在异步 function 之外等待。

var newMes;
function generateNewQuote() {
fetch('https://api.whatdoestrumpthink.com/api/v1/quotes/random')
       .then(response => response.json())
       .then(data => {
        newMes = data.message;
        // console.log(newMes);
    });
}
generateNewQuote();
setTimeout(function() {console.log(newMes)}, 2000)

The code above basically waits for a while before putting newMes into the console.上面的代码在将 newMes 放入控制台之前基本上要等待一段时间。 Because the problem of newMes being undefined is that the script is still fetching data from the server and thus requires a bit of time.因为 newMes 未定义的问题是脚本仍在从服务器获取数据,因此需要一些时间。

The problem of this is that 2000 milliseconds may not be enough to load the data and thus console.log(newMes) will return undefined.问题在于 2000 毫秒可能不足以加载数据,因此 console.log(newMes) 将返回 undefined。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 POST 请求中的数据保存为变量,以在 GET 请求中使用? - How do I save my data from my POST request, as a variable, to use in my GET request? 使用 Reactjs 获取 GET 请求 - 如何在我的请求中实现可重用组件来呈现我的数据? - Fetch GET request with Reactjs - how do i implement a reusable component within my request to render my data? 如何获得我的jquery帖子请求以与单击中从函数存储的变量一起使用? - How do I get my jquery post request to work with the variable stored from a function on a click? 如何从 Javascript 函数获取数据并将其保存到 php 中用户的自定义元字段? - How do I get data from Javascript function and save it to a user's custom meta field in php? Javascript-如何从setInterval()函数获取变量? - Javascript - How do I get a variable from a setInterval() function? 我如何从服务器获取JSON数据到javascript变量中 - How do I get JSON data from a server into a javascript variable 如何在Symfony中将javascript变量保存为表单类型 - How do i save a javascript variable to my form type in Symfony 如何将我的onclick事件链接到外部javascript函数? - How do I link my onclick event to an external javascript function? 如何让 Javascript 记住在我的 function 中重置但最初在我的 function 之外定义的变量? - How do I get Javascript to remember a variable that is reset in my function, yet initially defined outside of my function? 如何通过php从数据库获取数据到javascript变量? - How do I fetch data from database through php to javascript variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM