[英]Fetch API not giving response data in Node.js
我一直在嘗試解決這個問題,但它給了我這個: ReferenceError: json is not defined
這是我的代碼:
const fetch = require('node-fetch');
function makeAFile(text){
fetch("http://bin.shortbin.eu:8080/documents", {
method: "post",
body: 'hey wassup'
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.log(err))
return json
}
console.log(makeAFile('nope'))
您需要了解異步調用的工作原理。
const fetch = require('node-fetch');
function makeAFile(text){
fetch("http://bin.shortbin.eu:8080/documents", { // this is async call
method: "post", // might give result later in time
body: 'hey wassup'
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.log(err))
return json // JSON is not defined and will be return just after 'Fetch' is called which is 'undefined'
console.log(makeAFile('nope'));
要使您的代碼同步,您應該像這樣使用 async await:
const fetch = require('node-fetch');
async function makeAFile(text){
let res = await fetch("http://bin.shortbin.eu:8080/documents", { // await helps you to wait here to make your code sync
method: "post",
body: 'hey wassup'
})
const json = res.json()
return json ;
}
try {
const response = await makeAFile('nope')); //use try catch to catch error
console.log(response);
}
catch (error) {
console.log(`Error ${error}`);
}
您似乎對如何從異步調用返回數據感到困惑。 你可以使用這樣的回調來做到這一點:
const fetch = require('node-fetch');
function makeAFile(text){
return fetch("http://bin.shortbin.eu:8080/documents", {
method: "post",
body: text
})
.then(res => res.json())
.catch(err => { console.error(err) })
}
makeAFile('nope').then((result) => console.log(result));
你可以在這里閱讀更多信息;
編輯:使用async/await
提供解決方案
async function makeAFile (text) {
try {
const res = await fetch("http://bin.shortbin.eu:8080/documents", {
method: "post",
body: text
})
return res.json();
} catch (err) {
console.error(err);
}
}
// Define an anonymous async function to allow await statements inside
(async () => {
// this "await" here is important
const result = await makeAFile('nope');
console.log(result);
// now result is store inside of "result"
})();
您無權在外部訪問json
then
回調。 只需讓您的 function async
,這樣您就可以訪問它並且外部上下文代碼可以等到您的 function 將被解決或拒絕:
const fetch = require('node-fetch');
async function makeAFile(text){
const res = await fetch("http://bin.shortbin.eu:8080/documents", {
method: "post",
body: 'hey wassup'
})
const json = res.json()
console.log(json)
return json
}
並稱之為:
try {
const result = await makeAFile('nope')
console.log(result)
} catch(err) {
console.log(err)
}
或者
const result = makeAFile('nope').then(result => {
console.log(result)
})
.catch(err => {
console.log(err)
});
const fetch = require('node-fetch');
function makeAFile(text){
jsonOut = ''
fetch("http://bin.shortbin.eu:8080/documents", {
method: "post",
body: 'hey wassup'
})
.then(res => res.json())
.then(json => jsonOut = json)
.catch(err => console.log(err))
return jsonOut
}
console.log(makeAFile('nope'))
您無法訪問 promise 中的值。 只需檢查此代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.