首页 > 解决方案 > 试图从同一页面中的多个 api React native 获取结果

问题描述

我试图从数据库中获取数据并将其放入选择器中,当我使用一个 fecth 时它工作正常,但是当我添加第二个时,它得到一个错误提示“typeError: undefined is not an object (evalating 'this.state.dataSource2.地图') ”

componentDidMount() {

  fetch('url1')
    .then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        isLoading: false,
        dataSource: responseJson,

      }
      );
    })
     .then( () => {
      fetch('url2')
    .then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        isLoading: false,
        dataSource2: responseJson
      });
    })
    })
    .catch((error) => {
      console.error(error);
    });
}

第一个选择器:

        onValueChange={(itemValue, itemIndex) => this.setState({ar1: itemValue})} >

        { this.state.dataSource.map((item, key)=>(
        <Picker.Item label={item.nom} value={item.nom} key={key} />)
        )}

      </Picker>

第二个选择器:

        onValueChange={(itemValue, itemIndex) => this.setState({ar2: itemValue})} >

        { this.state.dataSource2.map((item, key)=>(
        <Picker.Item label={item.nom} value={item.nom} key={key} />)
        )}

      </Picker>

标签: react-native

解决方案


推荐阅读