Mongoose says a required path is missing when you save a document
ValidationErrorShort answer
The required validator fails when the value is undefined or null at save time. Almost always the value never reached the model: the request body was not parsed, the field name differs from the schema path, or an update set the field to null. Log the object before save and compare keys.
Fix it fast
1Find out which cause you have
Print exactly what you pass to the model, just before create or save. The missing key, or its undefined value, shows which cause below applies.
console.log('body:', req.body); console.log(Object.keys(req.body ?? {}));2Match what you see, then jump to its fix
- 1
The log shows body: undefined, or an empty object, while the client clearly sent dataThe request body was never parsed, so req.body is undefinedFix - 2
The log shows data, but the key is Name, user_name, or sits one level deeper than the schema expectsThe field name in the request differs from the schema pathFix - 3
The error appears on findOneAndUpdate or updateOne, not on save, and only when runValidators is onAn update with validators turns the field into null or removes itFix
- 1
What does the error look like?
ValidationErrorPath `name` is required.Path `email` is required.
Cause 1 of 3
The request body was never parsed, so req.body is undefined
1Check
Express leaves req.body undefined until a body-parsing middleware runs. That middleware must be registered before the route.
grep -n "express.json\|express.urlencoded" app.js server.js 2>/dev/null2Fix
Add the parsers above your routes. A form that uploads files is multipart, and that needs multer.
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/users', usersRouter);3Verify
Post again and check that the log now shows the fields.
curl -X POST http://localhost:3000/users -H 'Content-Type: application/json' -d '{"name":"Sam","email":"sam@example.com"}'Confirmed by expressjs.com
Cause 2 of 3
The field name in the request differs from the schema path
1Check
Mongoose reads the property named exactly like the schema path. A property with another name leaves the path undefined, and the required validator then fails.
console.log(Object.keys(User.schema.paths));
console.log(Object.keys(req.body));2Fix
Make both sides use the same key. Rename the form input or JSON property, or map it when you build the document.
const user = new User({
name: req.body.userName,
email: req.body.email,
});
await user.save();3Verify
Save again. A document without the error is returned.
console.log(await User.findById(user._id));Confirmed by mongoosejs.com
Cause 3 of 3
An update with validators turns the field into null or removes it
1Check
Update validators are off by default. When they are on, required fails only when the update explicitly sets the path to null or unsets it, so look for a value that becomes null on that route.
grep -rn "runValidators" src/ 2>/dev/null2Fix
Send only the fields that really change, and never send null for a required path. Drop empty values before the update.
const changes = Object.fromEntries(
Object.entries(req.body).filter(([, v]) => v !== null && v !== undefined)
);
await User.updateOne({ _id: id }, { $set: changes }, { runValidators: true });3Verify
Repeat the update with the same input. It saves without a validation error.
console.log(await User.findById(id));Confirmed by mongoosejs.com
How do I stop it coming back?
- Parse bodies once, at the top of the app, and keep route handlers free of manual parsing.
- Validate request input at the edge, so Mongoose is the second line of defence and not the first.
- Use one naming style for client keys and schema paths.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Express route crashes because req.body is undefined
TypeError: Cannot destructure property 'userId' of 'req.body' as it is undefined.
Express does not read request bodies on its own.
3 causes, each with a fixBack end
Express logs [Object: null prototype] instead of a plain object
TypeError: obj.hasOwnProperty is not a function
This is not an error.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fix