【发布时间】:2020-06-02 14:39:44
【问题描述】:
我正在开发一个 Reactjs 项目。登录后有登录/注册表单我可以在帐户页面中显示用户的数据。
在“帐户”页面上,用户将能够更新他在注册时填写的数据。
表格包含:
- 名字
- 姓氏
- 电子邮件
- 电话
- 收货地址
我想使用 Reactjs 表单更新数据。
我的代码
正面
class Account extends Component {
constructor() {
super();
this.state = {
first_name: "",
last_name: "",
email: "",
phone: "",
deliveryAddress: "",
errors: {}
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() {
const token = localStorage.usertoken;
const decoded = jwt_decode(token);
this.setState({
first_name: decoded.first_name,
last_name: decoded.last_name,
email: decoded.email,
phone: decoded.phone,
deliveryAddress: decoded.deliveryAddress
});
}
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
handleSubmit(e) {
e.preventDefault();
const { first_name, last_name, email, phone, deliveryAddress } = this.state;
const body = {
first_name,
last_name,
email,
phone,
deliveryAddress
};
const json = JSON.stringify(body);
console.log(json);
axios.put("http://localhost:5000/users/:id", json).then(res => {
console.log(res);
});
}
UserController.ts
export const updateUser = (req, res) => {
User.update(
{
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
phone: req.body.phone,
deliveryAddress: req.body.deliveryAddress
},
{
where: {
id: req.body.id
}
}
).then(user => {
res.json(user);
});
};
我认为我的问题在于我的 where 属性,因为当我点击保存按钮时,我得到了:Executing (default): UPDATE users SET updatedAt='2020-02-18 14:30:03' WHERE id = NULL 并且没有任何反应,没有任何更新。
编辑
UserController.ts
export const updateUser = (req, res) => {
const id = req.params.id;
User.update(
{
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
phone: req.body.phone,
deliveryAddress: req.body.deliveryAddress
},
{
where: {
id: id
}
}
).then(user => {
if (user == 1) {
res.send({
message: "User was updated successfully"
});
} else {
res.send({
message: `Cannot update User with id=${id}. Maybe User was not found or req.body is empty!`
});
}
});
};
当我在http://localhost:5000/users/10 上使用 Postman 和请求 PUT 时,我正确地更新了用户。我的问题是,如果我使用我的 Reactjs 代码,我将无法获得 id。
编辑 2
我在 state 和 componentDidMount() 函数中添加了 id 如下:
componentDidMount() {
const token = localStorage.usertoken;
const decoded = jwt_decode(token);
this.setState({
id: decoded.id,
first_name: decoded.first_name,
last_name: decoded.last_name,
email: decoded.email,
phone: decoded.phone,
deliveryAddress: decoded.deliveryAddress
});
}
handleSubmit(e) {
e.preventDefault();
const {
id,
first_name,
last_name,
email,
phone,
deliveryAddress
} = this.state;
const body = {
first_name,
last_name,
email,
phone,
deliveryAddress
};
const json = JSON.stringify(body);
console.log(json);
axios.put(`http://localhost:5000/users/${id}`, json).then(res => {
console.log(res);
});
}
我现在能够获取当前 id:Executing (default): UPDATE users SET updatedAt='2020-02-19 08:48:57' WHERE id = '15',但没有任何反应,因为 first_name 字段没有像我对 Postman 所做的那样修改:Executing (default): UPDATE users SET first_name='Quentin',updatedAt='2020-02-19 08:50:57' WHERE id = '15'
【问题讨论】:
-
你为什么要检查
user是否等于 1 成功?这似乎不对。 -
如果我更改任何字段,这将返回成功,如果 id 不存在则返回错误,它工作正常。
标签: node.js reactjs sequelize.js