Reactjs 无法读取未定义的属性映射

Reactjs 无法读取未定义的属性映射,reactjs,react-native,redux,Reactjs,React Native,Redux,我想使用地图功能在我的应用程序上显示数据 但它什么也没显示。 我正在使用Redux和MySQL数据库 我试图为(属性)设置默认状态,但它不起作用 这是组件代码 import React, {Component} from 'react'; import {ActivityIndicator,ScrollView,ImageBackground,TouchableOpacity,StyleSheet, Text, View,Button,TextInput,Image} from 'react-n

我想使用地图功能在我的应用程序上显示数据 但它什么也没显示。 我正在使用Redux和MySQL数据库

我试图为(属性)设置默认状态,但它不起作用

这是组件代码

import React, {Component} from 'react';
import {ActivityIndicator,ScrollView,ImageBackground,TouchableOpacity,StyleSheet, Text, View,Button,TextInput,Image} from 'react-native';
import {connect} from 'react-redux';
import {gethouse} from '../../store/actions/house_actions';
import {bindActionCreators} from 'redux';
class Property extends Component {
    static navigationOptions ={
        header:null
    }
    componentDidMount(){
         this.props.dispatch(gethouse());
    }
    postsProperty =(house) =>(
        house.property ?
            house.property.map((item,i)=>(
                <TouchableOpacity key ={i}>
                    <Text>hello</Text>
                </TouchableOpacity>
        ))
        :<ActivityIndicator/>
    )
    render() {
        return (
            <ScrollView style= {{backgroundColor:'#F0F0F0'}}>
                {this.postsProperty(this.props.House)}
            </ScrollView>

      );
    }
}
function mapstatetoprops(state){
    console.log(state);
    return{
        House:state.House
    }
}

export default connect(mapstatetoprops)(Property);
这是减速器

import {HOUSE_P} from '../types'
export default function(state={},action){
    switch(action.type){
        case HOUSE_P:
            return {...state,porperty: action.payload}
        default:
            return state;
    }
}
在调试器中,我正确地获取了数据 这是我的数据

如果我评论house.property?我收到了这个错误消息 typeError无法读取未定义的属性映射
顺便说一句,我显示文本“hello”只是为了知道它是否工作,它应该显示hello 3倍于我的数据库中的行数

您的
gethouse
函数正在使用axios执行异步调用,因此您正在返回一个挂起的承诺作为有效负载

我不知道你的减速机是什么样子,所以这是最好的猜测

您可以尝试将
gethouse
改为异步函数:

export async function gethouse(){
  try {
    const response = await axios({
      method:'GET',
      url:'http://192.168.1.4:3000/property'
    })

    console.log(response.data);
    const property = response.data;

    return {
      type: 'housing',
      payload: property
    }
  }
  catch(e) {
    console.log(e);
    return false;  
  }
}
…然后在组件中,在
gethouse
解决以下问题时分派操作:

componentDidMount(){
  gethouse()
    .then(action => this.props.dispatch(action));
}

我希望这能有所帮助。

它不起作用:\。如果需要,我在上面添加了reducer代码,我尝试放置这个控制台。警告(house.property)它显示:unfinedshould您的
mapstatetops
函数返回
{house:state.property}
而不是
{house:state.house}
componentDidMount(){
  gethouse()
    .then(action => this.props.dispatch(action));
}