javascript - 错误 : Actions may not have an undefined "type" property. 您是否拼写错误常量?

标签 javascript android react-native react-redux

我一生都无法弄清楚我做错了什么。我查看了其他人的帖子,与我的相似,找不到问题所在,有人可以帮忙吗?我昨天才开始 react 和函数式编程,所以很多东西对我来说都是新的。据我所知,我的行为一定有问题,或者我如何正确定义我的类型?这是我的代码。

index.android.js

function configureStore(initialState) {
    const enhancer = compose(
        applyMiddleware(
            thunkMiddleWare,
            loggerMiddleWare,
        ),
    );
    return createStore(reducer, initialState, enhancer)
}
const store = configureStore({});

const App = () => (
    <Provider store={store}>
        <AppContainer/>
    </Provider>
)

AppRegistry.registerComponent('BHPApp', () => App);

以及我的组件、 reducer 、操作和类型。

行动

import * as UserActions from './users'

export const ActionCreators = Object.assign({},
    UserActions
);

export const SET_ALL_USERS = 'SET_ALL_USERS';

import * as types from './types'
import ApiUtils from '../lib/apiUtils'

export function fetchAllUsers() {
    return (dispatch, getState) => {
        return fetch(URL + '/user/getAll')
            .then(ApiUtils.checkStatus)
            .then(response => {
                dispatch(setAllUsers({ hBaseUsers: response }));
            })
            .catch(e => {
                console.log(e)
            })
    }
}

function setAllUsers( {hBaseUsers} ) {
    hBaseUsers.forEach((hBaseUser) => {
        console.log(hBaseUser);
    });
    return {
        types: types.SET_ALL_USERS,
        hBaseUsers,
    }
}

reducer

import { combineReducers } from 'redux'
import * as userReducer from './users'

export default combineReducers(Object.assign(
    userReducer,
));

import createReducer from '../lib/createReducer'
import * as types  from '../actions/types'

export const getUsers = createReducer({}, {
    [types.SET_ALL_USERS](state, action){
        let newState = {};
        action.hBaseUsers.forEach( (hBaseUser) => {
           newState[hBaseUser.personUniqueID] = hBaseUser;
        });
        return newState;
    }
});

export const userCount = createReducer({}, {
    [types.SET_ALL_USERS](state, action){
        action.hBaseUsers.length;
    }
});

容器

class AppContainer extends Component{
    render(){
        return <Home {...this.props} />
    }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators(ActionCreators, dispatch);
}

    export default connect((state) =>  { return {} }, mapDispatchToProps)(AppContainer);
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
    ScrollView,
    View,
    TextInput,
    Image,
    TouchableHighlight,
    StyleSheet,
    Text
} from 'react-native'

class Home extends Component{
    searchPressed(){
        this.props.fetchAllUsers();
    }

    hBaseUsers(){
        return Object.keys(this.props.getUsers).map( key =>  this.props.getUsers[key])
    }

    render(){
        console.log(this.hBaseUsers());
        return<View>
            <View>
                <TouchableHighlight onPress={() => this.searchPressed() } >
                    <Text>Fetch Users</Text>
                </TouchableHighlight>
            </View>
            <ScrollView>
                {this.hBaseUsers().map((hBaseUser) => {
                    return <View>
                        <Text>
                            hBaseUser.personUniqueID
                        </Text>
                    </View>
                })}
            </ScrollView>
        </View>
    }
}

function mapStateToProps(state) {
    return{
        getUsers: state.getUsers
    }
}

export default connect(mapStateToProps)(Home);

在我读完所有内容之后,我的问题可能出在这些事情中,但我似乎找不到它。据我所知,我正确地拼写和定义了所有内容。有人可以帮我吗?

最佳答案

您的 setAllUsers 操作返回的对象具有错误的属性集:

function setAllUsers( {hBaseUsers} ) {

return {
    types: types.SET_ALL_USERS,
    hBaseUsers,
}

}

在返回语句中,将“types”更改为“type”以修复此错误。

关于javascript - 错误 : Actions may not have an undefined "type" property. 您是否拼写错误常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44273108/

相关文章:

react-native - View 在 React Native iOS 上无缘无故地抖动

javascript - jQuery 视频播放器的全屏切换未正确分配类名

JavaScript 改变了我的 CSS

Javascript 递归函数。将数据存储在数组中时出现错误结果

android - Android Studio 中的文件打印向导

android - 在 android 中使用矢量图像在 Real Device 上出现问题。 SVG-安卓

javascript - 使用 Jquery 访问 innerhtml

android - 嵌套的RecyclerViews MVP实现

React-Native 图像不透明度赋予文本不透明度

ios - react native : how to get the names of all the albums