CopyPastor

Detecting plagiarism made easy.

Score: 0.8382732426262882; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2022-11-18
by Vishal

Original Post

Original - Posted on 2013-02-18
by JohnnyHK



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Mongoose's findById method casts the id parameter to the type of the model's _id field so that it can properly query for the matching doc. This is an ObjectId but "foo" is not a valid ObjectId so the cast fails.
This doesn't happen with 41224d776a326fb40f000001 because that string is a valid ObjectId.
One way to resolve this is to add a check prior to your findById call to see if id is a valid ObjectId or not like so:
if (id.match(/^[0-9a-fA-F]{24}$/)) { // Yes, it's a valid ObjectId, proceed with `findById` call. }
Mongoose's `findById` method casts the `id` parameter to the type of the model's `_id` field so that it can properly query for the matching doc. This is an ObjectId but `"foo"` is not a valid ObjectId so the cast fails.
This doesn't happen with `41224d776a326fb40f000001` because that string is a valid ObjectId.
One way to resolve this is to add a check prior to your `findById` call to see if `id` is a valid ObjectId or not like so:
<!-- language: lang-js -->
if (id.match(/^[0-9a-fA-F]{24}$/)) { // Yes, it's a valid ObjectId, proceed with `findById` call. }

        
Present in both answers; Present only in the new answer; Present only in the old answer;