NodeJS Mongoose问题:Cannot read property 'email' of undefined报错与POST请求阻塞故障排查
Hey Sarah, let's fix those two frustrating issues you're hitting with Node.js and Mongoose one by one:
Issue 1: Cannot read property 'email' of undefined
This error happens because req.body is coming back as undefined—your app isn't parsing the incoming request body correctly. The problem is in your route file: you created a brand new express() instance there, and added the body-parsing middleware (express.json()/express.urlencoded()) to that unused instance instead of your main app or router. That middleware never actually runs for your requests!
Fix:
- Clean up your route file—remove the extra
express()instance and unused middleware:
const express = require('express') const router = express.Router(); const Model = require('../Models/Users'); router.post("/register/", async(req, res) => { const model = new Model({ email: req.body.email, password: req.body.password, }); const result = await model.save(); return res.json({ "Message": true }); }); module.exports = router;
- Add body-parsing middleware to your main app (this ensures all routes can access parsed request bodies):
const express = require("express"); const app = express(); const mongoose = require('mongoose'); const register = require('./UserRoutes/register'); // Add these two lines to parse JSON and form data app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(register); // Don't forget to start your server! const PORT = 3000; app.listen(PORT, () => { console.log(`Server up on port ${PORT}`); }); // Also, connect Mongoose to your database (missing in your original code!) mongoose.connect('mongodb://localhost:27017/your-database-name') .then(() => console.log('Connected to MongoDB successfully')) .catch(err => console.error('MongoDB connection failed:', err));
Issue 2: POST requests hanging in Postman
Your requests are hanging because you're not handling errors in your async route handler. If model.save() fails (e.g., no database connection, invalid data), the promise rejects but there's no catch block to send an error response—so the request waits forever. Plus, your original main file didn't start the server at all, which would also cause hanging requests.
Fix:
Wrap your async code in a try/catch block to ensure you send a response no matter what:
router.post("/register/", async(req, res) => { try { const model = new Model({ email: req.body.email, password: req.body.password, }); const result = await model.save(); return res.json({ "Message": true, user: result }); } catch (err) { // Send an error response instead of letting the request hang return res.status(500).json({ "Message": false, error: err.message }); } });
Key Takeaways:
- Never create a new
express()instance in route files—use theRouterinstance instead - Always parse request bodies with
express.json()/express.urlencoded()at the main app level - Always add error handling to async route handlers (try/catch is the simplest way)
- Don't forget to start your server with
app.listen()and connect Mongoose to your database
内容的提问来源于stack exchange,提问作者Sarah

