【发布时间】:2021-03-25 21:18:00
【问题描述】:
export class Diet extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
searchValue: "",
};
}
updateSearch = (value) => {
this.setState({ searchValue: value });
if (value.trim() !== "") {
axios
.get(
`https://api.spoonacular.com/food/products/search?apiKey=1234&query=${value}&number=100`
)
.then((res) => {
this.setState({ data: res.data });
})
.catch((error) => {
console.log(error.response.data);
});
} else {
setState({ data: [] });
}
}}
render() {
const {
data,
searchValue,
} = this.state;
return (
<SearchBar
placeholder="Search Food..."
onChangeText={this.updateSearch}
value={searchValue}
<List style={{ paddingTop: hp("2%") }}>
<TouchableOpacity>
{this.state.data.map(({ type }) => (
<Text>{this.state.type.products.title}</Text>
))}
</TouchableOpacity>
</List>
您好,我正在尝试使用 SearchBar 和 axios 从 Spoonacular API 获取数据,运行代码时出现以下错误:undefined is not a function (near '...this.state.data.map...')
数据库文档链接:https://spoonacular.com/food-api/docs#Search-Grocery-Products
【问题讨论】:
-
看起来您实际上并没有在映射数据中使用迭代值。尝试将
this.state.type.products.title换成type.products.title。 -
谢谢你的回答,但是没有用
标签: reactjs react-native api search axios