MERN全栈用户验证接口Promise拒绝问题求助
Hey there! Let's break down why your Promise is getting rejected and get this user verification working properly. I've spotted a few key issues in your code that are causing the problem:
1. Token is Being Pulled From the Wrong Place
Your frontend sends the token as a URL parameter (you're using /${token} in the axios post URL), but your backend is trying to fetch it from req.body.token instead of req.params.token. This means your Veri.findOne query is always returning null, which triggers unhandled logic and eventually a rejected Promise.
2. Undefined errors Variable
You're using an errors object throughout your route handler, but you never declare it first. This will throw a ReferenceError immediately, which is definitely causing your Promise to reject.
3. Vague Error Handling
Your catch block only logs "Promise Rejected" without any details about what went wrong. You can't debug what you can't see—you need to log the actual error object to understand the root cause.
4. Nested Promise Anti-Pattern
Nested .then() calls make code hard to read and easy to miss error cases. Switching to a chained Promise flow (or async/await) will make your code cleaner and error handling more consistent.
Modified Node.js Route Code
Here's the fixed version of your route with all the above issues addressed:
router.post("/confirmation/:token", (req, res) => { // Initialize errors object first const errors = {}; Veri.findOne({ token: req.params.token }) // Use req.params.token instead of 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); } return 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; return user.save(); }) .then(savedUser => res.json(savedUser)) .catch(err => { console.log("Promise Rejected:", err); // Log the actual error errors.server = "An unexpected error occurred during verification"; res.status(500).json(errors); }); });
Quick Frontend Check
Make sure that this.props.token in your Confirmation component is actually populated with the correct token from the URL. You can add a console log in componentDidMount to verify this:
componentDidMount() { if (this.props.auth.isAuthenticated) { this.props.history.push("/dashboard"); } // Verify token is present console.log("Verification token:", this.props.token); }
How to Test
- First, test the route directly with a tool like Postman to confirm it works when you send a valid token (as a URL param) and matching email in the request body.
- Once the route works standalone, test it through your React frontend to ensure the token is being passed correctly from the URL to the axios request.
内容的提问来源于stack exchange,提问作者Naeem

