【问题标题】:undefined is an object error in react-nativeundefined 是 react-native 中的对象错误
【发布时间】:2017-03-09 07:31:32
【问题描述】:
  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 不是一个对象(评估“this.state.responsedata.entryData”)。我已经在构造函数中声明了 EntryDate ,仍然得到同样的错误。上面的代码有什么问题?如何解决?

【问题讨论】:

    标签: react-native fetch


    【解决方案1】:

    使用

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

    【讨论】:

      【解决方案2】:

      我认为你应该使用

      &lt;Text&gt;{this.state.entryDate}&lt;/Text&gt;

      而不是

      &lt;Text&gt;{this.state.responsedata.entryDate}&lt;/Text&gt;

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

      【讨论】:

      • 因为你没有给 this.state.entryDate 设置任何数据
      • 如何设置数据?但数据没有设置?
      • 在 _onPressButtonGET () 中,您将整个响应设置为 this.state.data。只需提取 entryDate 并将其放入 this.state.entryDate 中,如下所示: this.setState({ entryDate: data_you_have }) 或在您的渲染方法中使用 this.state.data
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2016-10-14
      • 2022-01-23
      相关资源
      最近更新 更多