使用 express 更新 MongoDB 中的用户数据



嗨,伙计们,我尝试为我的应用程序创建一个更新函数,但我最终得到一个错误,说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);

从用户操作更新操作.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
}
}

这是我的用户数据缩减器.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;
}
}

这是我的全名.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)

在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());
};
}

相关内容

最新更新