【发布时间】:2019-03-30 04:15:30
【问题描述】:
我对 MERN 全栈了解不多。在这里,我在 nodejs 中有一条从反应前端获取令牌的路由,它在 mongoDb 中找到所需的用户后简单地标记“isVerified = true”。但相反,我收到错误消息,说我的承诺被拒绝了。我不知道是什么原因造成的。我附上了下面的代码。请帮我解决这个问题。
我尝试了多种方法,例如更改请求正文等。但对我没有任何效果。
这是调用路由的动作。
export const verifyUser = (verifyEmail, token, history) => dispatch => {
axios
.post(`/api/users/confirmation/${token}`, verifyEmail)
.then(res => history.push("/login"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
这是我的反应表单,要求输入电子邮件,然后进行验证。
import React, { Component } from "react";
import PropTypes from "prop-types";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import { verifyUser } from "../../actions/authActions";
import TextFieldGroup from "../common/TextFieldGroup";
class Confirmation extends Component {
constructor() {
super();
this.state = {
email: "",
errors: {}
};
this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
componentDidMount() {
if (this.props.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({ errors: nextProps.errors });
}
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
onSubmit(e) {
e.preventDefault();
const verifyEmail = {
email: this.state.email
};
this.props.verifyUser(verifyEmail, this.props.token, this.props.history);
}
render() {
const { errors } = this.state;
return (
<div className="confirmation">
<div className="container">
<div className="row">
<div className="col-md-8 m-auto">
<h1 className="display-4 text-center">User verification</h1>
<p className="lead text-center">Enter your email to verify</p>
<form noValidate onSubmit={this.onSubmit}>
<TextFieldGroup
placeholder="Email"
name="email"
type="email"
value={this.state.email}
onChange={this.onChange}
error={errors.email}
/>
<input type="submit" className="btn btn-info btn-block mt-4" />
</form>
</div>
</div>
</div>
</div>
);
}
}
Confirmation.propTypes = {
auth: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
errors: state.errors
});
export default connect(
mapStateToProps,
{ verifyUser }
)(withRouter(Confirmation));
这是路由器
router.post("/confirmation/:token", (req, res) => {
Veri.findOne({ token: req.body.token })
.then(token => {
if (!token) {
errors.email =
"We were unable to find a valid token. Your token may have expired";
return res.status(400).json(errors);
}
User.findOne({ _id: token._userId, email: req.body.email }).then(user => {
if (!user) {
errors.email = "We were unable to find a user for this token";
return res.status(400).json(errors);
}
if (user.isVerified) {
errors.email = "This user has already been verified";
return res.status(400).json(errors);
}
// Verify and save the user
user.isVerified = true;
user.save().then(user => res.json(user));
});
})
.catch(function() {
console.log("Promise Rejected");
});
});
【问题讨论】:
-
您应该在 catch 中记录拒绝的原因并尝试分析发生了什么。至少在此处粘贴错误。
-
您已将
confirmation路由设置为读取query param,但在您的路由正文中,您正在从req.body读取。将其更改为req.param.token
标签: node.js reactjs react-redux