Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用Fetch节点存储get调用的结果_Javascript_Node.js_Ajax_Promise_Es6 Promise - Fatal编程技术网

Javascript 无法使用Fetch节点存储get调用的结果

Javascript 无法使用Fetch节点存储get调用的结果,javascript,node.js,ajax,promise,es6-promise,Javascript,Node.js,Ajax,Promise,Es6 Promise,我正在使用Fetch节点获取服务 const response = await fetch("MY URL", { method: 'GET', headers: { 'Content-Type': 'application/json', }, timeout: 5000, }).then(res => res.json()).then(json => console.log(json)

我正在使用Fetch节点获取服务

 const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000,
      }).then(res => res.json()).then(json => console.log(json));
      console.log(response);
我将结果记录在第二个
console.log()
中,然后一切正常。 但是,当涉及到第二个
console.log()
时,响应是未定义的。 我需要的是第二个日志中要存储在响应中的内容


我所做的有什么问题吗?

您应该在函数中返回值

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000,
      }).then(res => res.json()).then(json => { 
         // do something 
         return json  //<--- return a value
        });
      console.log(response);
const response=wait fetch(“我的URL”{
方法:“GET”,
标题:{
“内容类型”:“应用程序/json”,
},
超时:5000,
}).then(res=>res.json()).then(json=>{
//做点什么

return json/您应该在函数中返回值

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000,
      }).then(res => res.json()).then(json => { 
         // do something 
         return json  //<--- return a value
        });
      console.log(response);
const response=wait fetch(“我的URL”{
方法:“GET”,
标题:{
“内容类型”:“应用程序/json”,
},
超时:5000,
}).then(res=>res.json()).then(json=>{
//做点什么

return json/如上所述,您没有返回
response
的值,因此它将不等于任何值。您可以
返回
最终
中的json,然后
,或者如果您觉得它更干净,只需
等待
而不是使用
。然后再

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000
      });

const json = await response.json();

console.log(json);

如前所述,您没有返回
响应的值,因此它将不等于任何值。您可以
返回
最终
中的JSON,然后
,或者如果您觉得它更干净,只需等待
而不是使用
。然后再

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000
      });

const json = await response.json();

console.log(json);

您可以使用async/await编写整个代码。在您的代码中,您混合了promise和async/await语法,并且忘记了返回上一个
函数中的
json
。then()
函数

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000,
      }).then(res => res.json()).then(json => { 
         // do something 
         return json  //<--- return a value
        });
      console.log(response);
以下是我编写代码的方式:

异步函数fetchData(){ const response=等待获取(“https://jsonplaceholder.typicode.com/todos/1", { 方法:“GET”, 标题:{ “内容类型”:“应用程序/json”, }, 超时:5000, }) const data=wait response.json(); 控制台日志(数据); }
fetchData();
您可以使用async/await编写整个代码。在您的代码中,您混合了promise和async/await语法,并且忘记了从上一个
返回
json
。然后()
函数

const response = await fetch("MY URL", {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 5000,
      }).then(res => res.json()).then(json => { 
         // do something 
         return json  //<--- return a value
        });
      console.log(response);
以下是我编写代码的方式:

异步函数fetchData(){ const response=等待获取(“https://jsonplaceholder.typicode.com/todos/1", { 方法:“GET”, 标题:{ “内容类型”:“应用程序/json”, }, 超时:5000, }) const data=wait response.json(); 控制台日志(数据); }
fetchData();
然后不只是记录而是返回它…@Andreas你的意思是返回json?然后不只是记录而是返回它…@Andreas你的意思是返回json?有趣的是,我相信你前几天在另一个问题的评论中给了我一些关于
fetch
的提示,现在我们提交相同的答案:)@TylerRoper我没有工作…所以在互联网上尝试一切…:)有趣的是,我相信你在前几天的另一个问题的评论中给了我一些关于
fetch
的提示,现在我们提交相同的答案:)@TylerRoper我没有工作…所以在互联网上尝试一切…)