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

When define const isValidObjectId = mongoose.isValidObjectId, isValidObjectId always returns false #11304

Closed
duyhcmut opened this issue Feb 1, 2022 · 1 comment

Comments

@duyhcmut
Copy link

duyhcmut commented Feb 1, 2022

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When passing an ObjectId to isValidObjectId from mongoose.isValidObjectId, it always returns false, no matter what the passed ObjectId has type of string or is an instance of ObjectId.
If the current behavior is a bug, please provide the steps to reproduce.

To reproduce, run this code:

const assert = require('assert');
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const isValidObjectId = mongoose.isValidObjectId;
assert(mongoose.isValidObjectId("5f5f1b2dc802bc22d44a0c2e")); //-> true
assert(isValidObjectId("5f5f1b2dc802bc22d44a0c2e")); //-> false
assert(mongoose.isValidObjectId(new ObjectId("5f5f1b2dc802bc22d44a0c2e"))); // -> true
assert(isValidObjectId(new ObjectId("5f5f1b2dc802bc22d44a0c2e"))); // ->false
What is the expected behavior?
It should be true.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 6.1.9
Nodejs 17.3.0

@duyhcmut duyhcmut changed the title When define const isValidObjectId = mongoose.isValidObjectId, isValidObjectId always return false When define const isValidObjectId = mongoose.isValidObjectId, isValidObjectId always returns false Feb 1, 2022
@pnutmath
Copy link
Contributor

pnutmath commented Feb 1, 2022

@duyhcmut This is because latest change in v6.1.9 of mongoose. inside isValidObjectId. this is getting referred which is undefined if you use like you mentioned.

I have created PR for this here #11305

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants