首页 > 解决方案 > 使用 express 更新 MongoDB 中的 userData

问题描述

大家好,我尝试为我的应用程序创建一个更新功能,但我最终得到一个错误,指出getState is not a function. (In 'getState()','getState' is 'Stephanson')哪个是 lastName 值。

我将在后端发布来自 Authentication_controller.js 的更新控制器

// Update user data
exports.update = function (req, res, next) {
  res.json({ data: req.user });
}

这是我的路由器也来自后端

router.route('/users/:user_id/data')
  .put(requireAuth, AuthenticationController.update);

从 userActions.js 更新操作

export function updateUserData(dispatch, getState) {
  const state = getState();
  const {user_id, token} = state.auth;
    return axios.put(USER_DATA(user_id), {
      headers: { authorization: token }
    }).then((response) => {
      console.log('my data = ' + response.data.data.userData[0].firstName);
      dispatch(setUserData(response.data.data.userData[0]));
    }).catch((err) => {
      dispatch(console.log("Couldn't update user data."));
    });
}

export var setUserData = (userData) => {
  return {
    type: 'SET_USER_DATA',
    userData
  }
}

这是我的 userDatareducer.js

module.exports = (state = [], action) => {
  switch (action.type) {
    case 'SET_USER_DATA':
      return action.userData;

    case 'UPDATE_USER_DATA':
      return action.userData;

    default:
      return state;
  }
}

这是我的 FullName.js 页面,我在其中使用来自 redux 表单的向导表单

const onSubmit = (values, dispatch) => {
    console.log(values);
    const firstName = values.firstName;
    const lastName = values.lastName;
    dispatch(updateUserData(firstName, lastName));
};
const FullName = props => {
    const { handleSubmit } = props;
    return (
        <View style={{ flex: 1 }}>
            <ScrollView style={{ backgroundColor: '#ffffff' }}>
                <View style={{ flex: 1, flexDirection: 'column', marginTop: 0, margin: 40 }}>
                    <Text style={{ fontSize: 40, marginTop: 60 }}>Full Name</Text>
                    <Text style={{ fontSize: 20, marginTop: 10 }}>Can you please tell your name?</Text>
                    <View style={{ marginTop: 100 }}>
                        <Field keyboardType='default' label='First Name' component={renderFieldFn} name='firstName' />
                        <Field keyboardType='default' label='Last Name' component={renderFieldLn} name='lastName' />
                    </View>
                </View>
            </ScrollView>
            <TouchableHighlight onPress={handleSubmit(onSubmit)} style={{ backgroundColor: 'white', padding: 20, alignItems: 'center' }}>
                <Text style={{
                    backgroundColor: 'black', color: 'white', fontSize: 20, fontWeight: 'bold',
                    height: 50, width: 300, textAlign: 'center', padding: 14
                }}>NEXT</Text>
            </TouchableHighlight>
        </View>
    );
}
export default reduxForm({
    form: 'newUser',
    destroyOnUnmount: false,
    forceUnregisterOnUnmount: true,
    validate
})(FullName)

标签: javascriptexpressreact-nativereduxredux-thunk

解决方案


在 userActions.js 中,尝试更改

export function updateUserData(dispatch, getState) {

export const updateUserData = (firstName, lastName) => (dispatch, getState) => {

由于 redux-thunk 返回一个创建操作的函数,因此上面的内容类似于文档中的这个示例:

function incrementIfOdd() {
  return (dispatch, getState) => {
    const { counter } = getState();

    if (counter % 2 === 0) {
      return;
    }

    dispatch(increment());
  };
}

推荐阅读