【问题标题】:react-native Can't display value got in my fetchreact-native 无法显示我获取的值
【发布时间】:2018-03-07 19:15:00
【问题描述】:

我无法显示从我的 fetch 中检索到的值到我的

这是我的代码,但我得到'null is not an object':

import React, {Component} from 'react';
import { StyleSheet, Image, Text, View, TextInput, StatusBar, Button,         
AppRegistry, TouchableHighlight, TouchableOpacity  } from 'react-native';
import AndroidBackButton from 'react-native-android-back-button'
import { StackNavigator } from 'react-navigation'

export default class ComponenFive extends React.Component {

getInfos(){
    fetch('http://172.16.101.183:3000/users', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            requestType: 'infosCompte'
        })
    })            
    .then((response) => response.json())
    .then((res) => {
            this.setState({username: res.username, nom: res.nom, prenom: res.prenom, lieu: res.lieu, batiment: res.batiment, bureau: res.bureau});
    })
    .done();
}


render() {
    {this.getInfos}
    return ( 
        <View style={{backgroundColor: 'white', flex: 1}}>
            <Text>Identifiant : {this.state.username}</Text>
        </View>
    )
}

}

你有什么想法可以帮助我吗?

【问题讨论】:

    标签: react-native fetch render state


    【解决方案1】:

    res 已经是 json。直接从资源中提取数据。

    console.log(res);
    let resData = JSON.parse(res._bodyText);
    this.setState({username: resData.username, nom: resData.nom, .....})
    

    【讨论】:

      【解决方案2】:

      使用 fetch 您获得的不仅仅是 json 上的数据。

      试试这个例子

      fetch('http://172.16.101.183:3000/users', {
              method: 'POST',
              headers: {
                  'Accept': 'application/json',
                  'Content-Type': 'application/json',
              },
              body: JSON.stringify({
                  requestType: 'infosCompte'
              })
          })            
          .then(response => response.json().then(json => ({ json, response })))
          .then(({ json, response }) => {
              if (!response.ok) {
                  return Promise.reject(json);
              }
              return json;
          })  
          .then((res) => {
                  this.setState({username: res.username, nom: res.nom, prenom: res.prenom, lieu: res.lieu, batiment: res.batiment, bureau: res.bureau});
          })
          .done();
      

      更新:

      抱歉,刚刚看到您的代码,不正确。在 render() 上使用 {this.getInfos} 是错误的。 getInfo 只是一个函数。你不返回任何东西。如果您想按原样调用此函数,请使用 componentWillMount 并在构造函数上设置状态

      constructor(props) {
          super(props);
          this.state = {username: '', nom: ''};
      }
      componentWillMount() {
          this.getInfos()
      }
      

      【讨论】:

      • 感谢您的回答,但我仍然有同样的错误信息
      • 哦,我不知道!非常感谢它现在正在工作!
      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2016-11-02
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多