-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Add empty string check for collection name passed #14806
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vkarpov15
approved these changes
Aug 16, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find, thanks 👍 Empty collection name is a good case for us to handle, since MongoDB disallows empty collection names:
MongoDB Enterprise > db.createCollection('')
{
"ok" : 0,
"errmsg" : "Invalid namespace specified 'test.'",
"code" : 73,
"codeName" : "InvalidNamespace"
}
This was referenced Sep 13, 2024
This was referenced Sep 14, 2024
This was referenced Sep 14, 2024
This was referenced Sep 24, 2024
This was referenced Sep 25, 2024
This was referenced Sep 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@vkarpov15, I was unable to add reviewers directly and would appreciate your feedback on this PR. Please let me know if there are any additional steps I should follow or if you have any suggestions.
Feel free to reach out if you have any questions or need further information.
Summary
The motivation for me to make this pull request is that while we were working on our serverless project we used Mongoose but we got an error that said Cannot read properties of undefined (reading 'toLowerCase') we were not able to fix as on our local we could run and it worked fine so we scratched a lot of our head thinking we didn't use toString function anywhere where is the issue code, so we put consoles and then check on deployment logs and realized the issue was that the secret manager was not properly configured so let say we have process.env.COLLECTION_NAME will be undefined, so the problem was not handled and threw an error from the module where it tried to convert undefined to lowercase using the javascript method but it took a good amount of effort and time to fix it so I thought of understanding the flow and got to know the utils.js file has method that runs a function on collection name, that is toCollectionName so I put a string and empty string check here so meaningful message are thrown and error is immediately caught saving from a lot of frustration, effort and saving time, also I ran the test case to confirm nothing breaks but found some lines were not covered in the test so added some test cases to cover them as well.
Examples
The demonstration is passing values other than string will give Collection name must be a string error message and if it is a string but empty then it will say Collection name cannot be empty.
To test after making a successful mongoose connection:
const User = mongoose.model('', userSchema); or const User = mongoose.model(undefined, userSchema);
Earlier it used to throw a toLowerCase error but now with the above-referenced message will be thrown
Please do let me know if you have any queries regarding, suggestions, feedback, or enhancement to make.