【发布时间】:2020-01-03 02:27:41
【问题描述】:
我正在使用 Fetch API 从 API 的端点获取一些数据。
我收到此错误:
Uncaught (in promise) TypeError: package.map is not a function
我做了一些研究,似乎发生错误是因为响应是对象而不是数组。
这是一个代码示例:
const url = 'https://reqres.in/api/users?page=2';
const fetchPromise = fetch(url);
fetchPromise.then(response => {
return response.json();
}).then(people => {
// checks response value type
console.log(typeof people);
const names = people.map(person => person.name).join('\n');
});
你可以找到我的sample code here。
【问题讨论】:
-
你能检查一下你从那个网址的回复吗?
-
正确,响应是一个对象而不是数组。目前还不清楚您期望地图会做什么。
标签: javascript arrays object promise fetch