【问题标题】:Axios to get data from databaseaxios从数据库中获取数据
【发布时间】:2021-03-24 04:41:55
【问题描述】:
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>

嗨,我正在尝试使用 axios 从 Spoonacular 数据库中获取数据,我正在尝试使用 SearchBar 搜索食物并在 List 中显示内容,我是编程新手而且我不太确定自己在做什么,当我运行代码时它告诉我undefined is not an object (evaultaing 'data.parsed.map'),这也是使用SearchBar 获取数据的正确方法。

文档链接:https://spoonacular.com/food-api/docs#Search-Grocery-Products

【问题讨论】:

    标签: reactjs react-native api axios


    【解决方案1】:

    你只是错过了 this.state.data

                 <TouchableOpacity>
                    {this.state.data.map(({ type }) => (
                      <Text>{this.state.type.products.title}</Text>
                    ))}
                  </TouchableOpacity>
     

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2021-07-22
      • 2021-08-30
      • 1970-01-01
      • 2019-03-14
      • 2020-06-19
      • 2021-02-08
      • 2021-09-22
      • 2019-06-27
      相关资源
      最近更新 更多