javascript - 使用 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)
解决方案
在 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());
};
}
推荐阅读
- java - 为什么标头值没有插入到 Spring DSL 中路由的 Apache Camel 的 `to` url 参数中?
- java - 如何使用 Android Studio 将 Admob 横幅添加到本机代码中的所有活动?
- wcf - Powerbuilder.net 不在 DotNet Web 服务 (WCF) 中组装
- angular - 如何将托管在 docker 容器中运行的 tomcat 上的角度前端连接到也在 docker 容器中运行的 springboot 后端?
- go - 如果列表项具有键==值,则从列表中获取值
- c# - 用c#中列表中的连续元素替换字符串占位符
- typescript - 如何使用自定义匹配器扩展 jest expect 的 extendExport typescript 命名空间
- three.js - 具有 JPG 纹理的自定义 three.js 着色器。每一帧的图像解码和 texTexture2D 调用都会导致大量丢帧
- flask - 有没有办法使用 wtforms 遍历 Flask 中的 Form 中的 FormField?
- php - 数据库中的数据不会传递给 laravel 中的 view.blade