Postman调用/users接口始终返回[],请求排查模型问题
Hey there! Let's break down why your GET /users endpoint is returning an empty [] even though your database connection checks out and basic string responses work. Since you suspect the User model is the culprit, that's spot-on—here are the most common issues to investigate with your UserModel.js and related code:
1. Mongoose Collection Name Mismatch
This is the #1 gotcha with Mongoose and MongoDB collections. By default, Mongoose automatically converts your model name to lowercase plural to map to a collection. So if your model is defined as:
const User = mongoose.model('User', userSchema);
Mongoose will look for a collection named users (all lowercase), but your collection is actually Users (capitalized).
Fix: Explicitly specify your collection name as the third argument when defining the model:
const User = mongoose.model('User', userSchema, 'Users');
2. Schema Field Case/Name Mismatch
MongoDB is case-sensitive with field names. If your UserModel.js schema uses field names that don't exactly match what's stored in your Users collection, Mongoose won't retrieve any data. For example:
- If your collection has a field
UserNamebut your schema definesusername - If your schema uses
emailbut the collection storesEmail
Double-check every field in your schema against the actual documents in MongoDB (use Compass or db.Users.findOne() in the shell to verify).
3. Incorrect Model Export/Import
Make sure your model is exported correctly from UserModel.js and imported properly in your controller:
- In
UserModel.js:// Ensure you're exporting the model correctly module.exports = mongoose.model('User', userSchema, 'Users'); - In
controllers/users.js:// Verify the path to your model is accurate (../model/UserModel vs ./model/UserModel) const User = require('../model/UserModel');
A wrong file path will lead to using an undefined model, which might silently return an empty array.
4. Faulty Query Logic in Controller
Even if the model is correct, a mistake in your controller's query can return empty results. Check:
- Are you using
User.find({})to fetch all users? If you added a filter like{ active: true }but none of your documents have that field/value, you'll get an empty array. - Are you handling async/await properly? Missing
awaitbeforeUser.find()will return a promise instead of data, which might be serialized to an empty array in some cases.
Example of correct query code:
'use strict'; const User = require('../model/UserModel'); exports.getUsers = async (req, res) => { try { const users = await User.find({}); // Empty object = fetch all documents res.json(users); } catch (error) { res.status(500).json({ message: error.message }); } };
5. Verify Collection Has Data
Before diving deeper into code, confirm your Users collection actually contains documents. Run this in the MongoDB shell:
use your-database-name db.Users.find()
If this returns nothing, the issue is that your collection is empty—not your code! If it does return documents, you can be certain the problem is in your model/query setup.
内容的提问来源于stack exchange,提问作者Athul Muralidharan

