2017-09-26 17 views
0
私はこれが私のコードですが、私が手に私の

値を表示することはできません反応し、ネイティブは

にフェッチ私から取得した値を表示することはできません

は「nullはオブジェクトではありません」フェッチ私の中で得ました:

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> 
    ) 
} 

}

あなたは私を助けるために任意のアイデアを持っていますか?

答えて

0

フェッチを使用すると、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(); 

はUPDATEこの例を試してみてください。

は申し訳ありませんが、ちょうどあなたのコードを見て、正しいではありません。 render()で{this.getInfos}を使うのは間違っています。 getInfoは単なる関数です。あなたは何も返さない。あなたはそれがcomponentWillMountを使用して、コンストラクタに

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

あなたの答えのおかげで、私はまだああ、私はそれを知らなかった –

+0

同じエラーメッセージを持っています!ありがとう、それは今働いています! –

+0

私の更新の答えをチェックし –

0

RESすでにJSONをセット状態であるとして、この関数を呼び出したい場合。直接resからデータをプルします。

console.log(res); 
let resData = JSON.parse(res._bodyText); 
this.setState({username: resData.username, nom: resData.nom, .....}) 
関連する問題