-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
findById returns "DocumentNotFoundError" instead of "CastError" with invalid ID #13165
Closed
2 tasks done
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
I check versions 5, 6, and 7 and they all returned a document not found error. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Test Testerson'
});
const doc = await Test.findById('123456789012').orFail();
}
run(); |
@IslandRhythms that's because |
brain fart. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Test Testerson'
});
const doc = await Test.findById('123456789012adff').orFail();
}
run(); |
vkarpov15
added a commit
that referenced
this issue
Mar 17, 2023
vkarpov15
added a commit
that referenced
this issue
Mar 21, 2023
vkarpov15
added a commit
that referenced
this issue
Mar 21, 2023
vkarpov15
added a commit
that referenced
this issue
Mar 21, 2023
fix(query): avoid executing transforms if query wasn't executed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
7.0.0
Node.js version
18.13.0
MongoDB server version
5.0.15
Typescript version (if applicable)
No response
Description
When using .findById method, an error named "DocumentNotFoundError" is returned when in previous versions an error with name "CastError" would be returned.
Steps to Reproduce
In the code below, when an invalid ID is passed in req.params.UserId a "DocumentNotFoundError" results. This causes the errorHandler function to return a return a status code of 404, when a status code of 400 would be more appropriate.
Expected Behavior
I expected an error name of "Cast Error", but instead received an error with a name "DocumentNotFoundError".
The text was updated successfully, but these errors were encountered: