2017-09-26 4 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를 사용하고 이미 생성자에 JSON을

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

귀하의 답변을 주셔서 감사합니다,하지만, 난 여전히 아 난 몰랐어 –

+0

같은 오류 메시지가! 고마워요. 지금 일하고 있어요! –

+0

내 업데이트 된 대답을 확인 –

0

고해상도를 상태를 설정 때문이다. res에서 직접 데이터를 가져옵니다.

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