javascript - response.json 未定义 express.js
问题描述
所以这是我现在的代码:
function geoPerformAction(e) {
getGeoApiData(document.getElementById('zipPostCode').value)
.then((APIarr) => {
postGeoData('/geoadd', { Lat: APIarr[0] });
})
.then(function () {
updateUIGeo();
})
}
//Friend helped with me with get API data
/* Function to GET Web API Data*/
const getGeoApiData = async ( place) => {
const response = await fetch("https://pokeapi.co/api/v2/pokemon/" + place + "/");
try {
const webData = response.json();
const Pla = webData;
console.log(Pla);
const APIarr = [Pla];
return APIarr;
}
catch (error) {
console.log("error", error);
}
}
每次我使用它时,webdata 变量都是未定义的。为什么会这样?为什么不返回我请求的数据?
谢谢你。
解决方案
你不是在等待第二个承诺得到解决
const webData = await response.json();
例子:
async function fetchAsync () {
// await response of fetch call
const response = await fetch('https://api.github.com');
// only proceed once promise is resolved
const data = await response.json();
// only proceed once second promise is resolved
return data;
}
推荐阅读
- javascript - 当节点具有坐标时,Vis.js 节点布局未按预期运行
- adobe-illustrator - Adobe illustrator 将文件名链接到图层名称脚本
- javascript - 如何将自定义方法重新加载到反序列化的 FabricJS 对象?
- visual-studio-code - 包装一些降价块
- adobe-indesign - 如何使用 python 程序从 indd 文件或 indesign 文件中提取文本?
- ckeditor - CKEditor 提到 outputTemplate 插入为 HTML 内容
- docker - 容器停止写入日志
- java - 可以在没有代理的情况下创建 KafkaReceiver 吗?
- string - 如何将字符串列表:'k:v',第一个字符串只有一个值,导入字典
- c# - 如果 BindingList 中的元素在 WPF ViewModel 上发生更改,则引发事件