从API获取日志但未呈现

时间:2020-01-28 12:57:47

标签: reactjs react-native react-native-android react-native-flatlist react-native-navigation

我一直在尝试从API提取数据,但是日志似乎显示了responseJSON,但是它没有出现在我的前端。

这是我获取api的代码:

componentDidMount() {
    return fetch('http://atiiuserauth.ap-south-1.elasticbeanstalk.com/home')
      .then(response => response.json())
      .then(responseJson => {
        this.setState(
          {
            isLoading: false,
            data: responseJson,
          },
          console.log(responseJson)
        );
      })
      .catch(error => {
        console.error(error);
      });
  }

这是我的reandermethod()

 render() {
    const { height } = Dimensions.get('window');
    const { width } = Dimensions.get('window');
    const like = this.state.liked ? 'red' : 'white';

    if (this.state.isLoading) {
      return (
        <View style={{ flex: 1, padding: 20 }}>
          <ActivityIndicator />
        </View>
      );
    }

    return (
      <View style={{flex:1}}>

        <FlatList
        datasource={this.state.data}

        renderItem={({ item }) => (
        <View style = {{alignContent: 'stretch'}}>
        <Video 
          source={{ uri: item.urlVid }}
          resizeMode = "cover"
          style={{width: "100%", height: 800}}
        />
         <View
              style={{
                position: 'absolute',
                flexDirection: 'column',
                alignItems: 'flex-end',
                top: '50%',
                right: 10,
              }}>
              <TouchableOpacity
                onPress={this.handleClick}
                style={{
                  alignItems: 'center',

                  borderRadius: 60,
                  padding: 10,
                }}>
                <Icon
                  name="heart"
                  size={30}
                  color={this.state.buttonColor}
                  onPress={this.onButtonPress}
                />
              </TouchableOpacity>


              <TouchableOpacity
                style={{
                  alignItems: 'center',

                  borderRadius: 60,
                  padding: 10,
                }}>
                <Icon name="share" size={30} color="white" />
              </TouchableOpacity>
              <Text style={{ right: 5, color: 'white' }} />
              <TouchableOpacity
                style={{
                  alignItems: 'center',

                  borderRadius: 60,
                  padding: 10,
                }}>
                <Icon name="whatsapp" size={30} color="white" />
              </TouchableOpacity>
              <Text style={{ right: 5, color: 'white' }} />
              <TouchableOpacity
                style={{
                  alignItems: 'center',

                  borderRadius: 60,
                  padding: 10,
                }}>
                <Icon name="download" size={30} color="white" />
              </TouchableOpacity>
              <Text style={{ right: 5, color: 'white' }} />
            </View>
            <View
              style={{
                position: 'absolute',
                flexDirection: 'column',

                top: '90%',
                left: 10,
              }}>
              <View
                style={{
                  flexDirection: 'row',
                }}>
                <Text
                  style={{ fontWeight: 'bold', fontSize: 20, color: 'white' }}>
                  {item.title} - {item.price}
                </Text>



              </View>

            </View>

        </View>


        )}
        ListHeaderComponent={this.header}
        stickyHeaderIndices={[0]}
        keyExtractor={ item => item.id}
      />

      </View>
    );
  }
}

日志为:

 LOG  {"product": [{"__v": 0, "_id": "5e30067e667d8473f8e79726", "color": "sasasa", "colors": [Array], "description": "sassa", "nameImg": "apex-legends-logo-1580205687689.jpg", "nameVid": "videoplayback-1580205687690.mp4", "sellerID": "sasa", "sellerName": "sasa", "size": "sasa", "sizes": [Array], "title": "wqw", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-28T10:01:34.987Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/apex-legends-logo-1580205687689.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback-1580205687690.mp4"}, {"__v": 0, "_id": "5e301696f75182463c6874ed", "color": "Space Grey", "colors": [Array], "description": "apple", "nameImg": "61jgfLBydjL._SL1024_-1580209807807.jpg", "nameVid": "videoplayback (1)-1580209807809.mp4", "price": 99900, "sellerID": "13755902031", "sellerName": "Appario", "size": "5.8-inch", "sizes": [Array], "title": "Apple iPhone 11 Pro", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-28T11:10:14.244Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/61jgfLBydjL._SL1024_-1580209807807.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback+%281%29-1580209807809.mp4"}, {"__v": 0, "_id": "5e30171df75182463c6874ee", "color": "Haze Blue", "colors": [Array], "description": "oneplus", "nameImg": "61FRLa8IFTL._SL1500_-1580209943294.jpg", "nameVid": "videoplayback-1580209943295.mp4", "price": 53999, "sellerID": "13755902031", "sellerName": "OnePlus", "size": "6.67 inch", "sizes": [Array], "title": "OnePlus 7T Pro", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-28T11:12:29.918Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/61FRLa8IFTL._SL1500_-1580209943294.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback-1580209943295.mp4"}], "user": {"__v": 0, "_id": "5e300846241a3b1c89d654c4", "address": [], "changes": [], "checkout": [], "like": [], "mobile": 8697779335, "registeredOn": "2020-01-28T10:09:10.569Z"}}

请提供帮助,并指出我的错误,请告诉我是否需要其他详细信息

1 个答案:

答案 0 :(得分:0)

您需要在FlatList中使用this.state.data.product,因为产品位于json响应的product字段中。

此外,您需要将数据设置为FlatList中的data属性,而不是数据源。

下面这行:

datasource={this.state.data}

必须是这样的:

data={this.state.data.product}