【发布时间】:2021-06-21 07:45:23
【问题描述】:
export class Diet extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
};
}
updateSearch = (e) => {
axios
.get(
`https://api.spoonacular.com/food/products/search?apiKey{1234}&number=100`
)
.then((res) => {
this.setState({ data: res.data });
});
};
render()
return(
<SearchBar
placeholder="Search Food..."
onChangeText={this.updateSearch}
value={data}
/>
<List style={{ paddingTop: hp("2%") }}>
<TouchableOpacity>
{this.state.data.map(({ type }) => (
<Text>{this.state.type.products.title}</Text>
))}
</TouchableOpacity>
</List>
大家好,我正在尝试使用 axios 从 Spoonacular 数据库中获取数据,我正在尝试使用 SearchBar 搜索食物并在列表中显示内容,我是编程新手我不太确定自己在做什么,当我运行代码时它告诉我[Unhandled promise rejection: Error: Request failed with status code 400] 并且列表上没有显示任何内容。
文档链接:https://spoonacular.com/food-api/docs#Search-Grocery-Products
【问题讨论】:
-
正如错误所说。请求失败,并且由于您没有添加
.catch,因此拒绝未处理。您可能没有正确请求 API。 -
谢谢,我添加了
.catch,但无法弄清楚API请求有什么问题
标签: reactjs react-native api axios unhandled-promise-rejection