undefined是react-native中的对象错误

时间:2017-03-09 07:31:33

标签: react-native fetch

  import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  TouchableOpacity,
  Image,
  TextInput,                                            
  TouchableHighlight,                                            
  Alert
} from 'react-native';

import Button from 'react-native-button'
import {Actions} from 'react-native-router-flux'
import Home from './Home'

export class Weight extends Component{
    constructor(props) {
  super(props);

  this.state = {
    data: '',
    data1:'',
    textinput:'',
    entryDate: '',
    systol:''
  }

  componentDidMount(){
    this._onPressButtonGET();
  } 
 _onPressButtonPOST(){
            fetch("url", {
                method: "POST",
                 headers: {
                     'Accept': 'application/json',
                     'Content-Type': 'application/json',
                 },
                body: JSON.stringify({
                    "entryDate":"3/2/2017 2:00 AM",
                    "systol": "120",
                    "mobileType":"ANDROID",
                    "userName":"menutest"

                })})
            .then((response) => response.json())
            .then((responseData) => {
                Alert.alert(
                    "Blood pressure data",
                    "Blood pressure data - " + JSON.stringify(responseData)
                )
            })
            .done();
        }

        _onPressButtonGET ()  {
            fetch("url", {
                method: "POST",
                 headers: {
                     'Accept': 'application/json',
                     'Content-Type': 'application/json',
                 },
                body: JSON.stringify({"mobileType":"ANDROID","userName":"menutest"})})
            .then((response) => response.json())
            .then((responseData) => {
                    this.setState({ data: JSON.stringify(responseData) })
                })

            .done();
        }
    render(){
            return(
                <View>
                    <TouchableHighlight onPress={this._onPressButtonPOST}>
                        <Text>Add</Text> 
                    </TouchableHighlight>


                     <TouchableHighlight onPress={this._onPressButtonGET.bind(this)}>
                        <Text>show</Text>
                       </TouchableHighlight>

                      <Text>{this.state.responseData.entryDate}</Text>
                      <Text>{this.state.responseData.systol}</Text>

                </View>
            );
        }
    }

    module.exports = Weight;

在上面的代码中,我觉得undefined不是一个对象(评估&#39; this.state.responsedata.entryData&#39;)。我在构造函数中声明了EntryDate,仍然得到相同的错误。上面的代码有什么问题?怎么解决?

2 个答案:

答案 0 :(得分:0)

我认为你应该使用

<Text>{this.state.entryDate}</Text>

而不是

<Text>{this.state.responsedata.entryDate}</Text>

检查并确定它是否会按预期工作。祝你好运

答案 1 :(得分:0)

使用

 <Text>{this.state.entryDate}</Text>