Javascript 无法使用Fetch节点存储get调用的结果
我正在使用Fetch节点获取服务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)
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我没有工作…所以在互联网上尝试一切…)