reactjs - TypeError:在获取外部 API 时无法读取 React 中未定义的属性“地图”
问题描述
我正在尝试使用外部 API - Unsplash in React 获取一些照片并得到错误 =(见标题)。API 似乎工作得很好,因为当我在控制台记录它时,但不知何故我无法将它打印出来。你能看看我是否在某个地方犯了错误吗?我真的迷路了,已经花了几个小时试图弄清楚。
这是我的代码:
import React, { useState } from "react";
import axios from "axios";
const PhotoList = () => {
const [photography, setPhotography] = useState("");
const [clientId, setClientId] = useState(
"MyAPIKey"
);
const [result, setResult] = useState([]);
const handleChange = ev => {
setPhotography(ev.target.value);
};
const handleSubmit = ev => {
console.log(photography);
const url =
"https://api.unsplash.com/photos?page=1&query" +
photography +
"&client_id=" +
clientId;
axios.get(url).then(response => {
console.log(response);
setResult(response.data.results);
});
};
并返回 HTML :
return (
<>
<h1>Here is the list of the photos</h1>
<input
onChange={handleChange}
type="text"
name="photography"
placeholder="Get more inspiration here"
></input>
<button onClick={handleSubmit} type="submit">
Search
</button>
{result.map(photography => (
<img src={photography.urls.small} />
))}
</>
);
};
export default PhotoList;
解决方案
查看 API 响应正文后,API 回调应为:
setResult(response.data);
API 响应正文中results
不存在密钥。
推荐阅读
- parallel-processing - 如何使用 Rayon 检测整数总和的溢出?
- android - 当特征图像为空时,我的 Android 应用程序被强制停止
- haskell - 如何在 Dhall 中定义和使用类似 Haskell 的 sum 类型
- tensorflow - 使用 Tensorflow 的稀疏神经网络
- python - 如何将数组写入 .txt 文件,然后用相同的 .txt 文件填充数组?
- c# - 如何使用刚刚创建的用户记录的 id 设置查找字段?
- express - 无法发布 /api/users/register
- artificial-intelligence - LSTM 权重维度如何取决于输入维度?
- mapbox-gl-js - 不同缩放级别的 map.querySourceFeatures
- javascript - Highcharts 多个系列强制 xAxis 起点