reactjs - Axios to get data from database
问题描述
export class Diet extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
};
}
updateSearch = (e) => {
axios
.get(
`https://api.spoonacular.com/food/products/searchapiKey{1234}&query=${this.state.data}&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>
{data.parsed.map(({ type }) => (
<Text>{this.state.type.products.title}</Text>
))}
</TouchableOpacity>
</List>
Hi, I'm trying to get data from the Spoonacular database using axios
, I'm trying to search the food with the SearchBar
and display the content in the List
, I'm new to programming and I'm not very sure of what I'm doing, when I run the code it tells me undefined is not an object (evaultaing 'data.parsed.map')
, also it this the right way to use SearchBar
to get data.
Link to the documentation: https://spoonacular.com/food-api/docs#Search-Grocery-Products
解决方案
you just miss this.state.data
<TouchableOpacity>
{this.state.data.map(({ type }) => (
<Text>{this.state.type.products.title}</Text>
))}
</TouchableOpacity>
推荐阅读
- apache-flink - Flink BroadcastProcessFunction vs CoProcessFunction
- c# - 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType?
- sharepoint-online - 通过 m365 spo 工具以编程方式填充 SharePoint Online 中的多选字段
- excel - VBA 求解器:在手动打开求解器选项对话框并选择确定之前无法正确求解
- python - 使用 Python 打开文件时执行两个单独的“功能”的问题
- conda - 安装在 WSL2 上的 Miniconda3:未找到 conda 命令
- javascript - 谷歌地图:JS vs Android/Flutter
- ssrs-2008 - SSRS tablix 在分页符处显示额外的空白行
- python - 当它显示 int() 以 10 为底的无效文字时我该怎么办。我已经仔细检查了整个语法并且没有错误信息
- scheme - 如何在方案中编写一个函数,在带有汽车和 cdr 的列表中找到一个元素?