Skip to content
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
DavidMiles1925 opened this issue Mar 13, 2023 · 3 comments · Fixed by #13185
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@DavidMiles1925
Copy link

DavidMiles1925 commented Mar 13, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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.

module.exports.getUserById = (req, res) => {
  User.findById(req.params.UserId)
    .orFail()
    .then((user) => {
      res.status(200).send(user);
    })
    .catch((err) => {
        errorHandler.errorHandler(req, res, err);
    });
};

Expected Behavior

I expected an error name of "Cast Error", but instead received an error with a name "DocumentNotFoundError".

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Mar 13, 2023
@IslandRhythms
Copy link
Collaborator

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();

@vkarpov15
Copy link
Collaborator

@IslandRhythms that's because 123456789012 is a valid string representation of an ObjectId. Why did you use that string exactly?

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Mar 14, 2023
@vkarpov15 vkarpov15 added this to the 7.0.3 milestone Mar 14, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 14, 2023
@IslandRhythms
Copy link
Collaborator

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants