Mongoose - 5.3.4 - Prevent cast to ObjectId for query with string












0















I am attempting to findOneAndUpdatea string-based token on a User model. And
I receive the error:



Cast to ObjectId failed for value "{ passwordResetToken: '4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7',
passwordResetExpires: { '$gt': 1543196590882 } }" at path "_id" for model "User"


the document is stored like so:



{
"_id": {
"$oid": "5bfb424da0cc0923f05b67f1"
},
"local": {
"email": "XXXXXXXXXXXXXXXXX",
"password": "XXXXXXXXXXXXXXXXX"
},
"isVerified": false,
"method": "local",
"__v": 0,
"passwordResetExpires": {
"$date": "2018-11-26T02:41:17.851Z"
},
"passwordResetToken": "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"
}


and I query the document like so:



req.params.token = "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"


User.findByIdAndUpdate({
'passwordResetToken': req.params.token,
'passwordResetExpires': { $gt: Date.now() }
},
{
'local.password' : req.body.password,
'passwordResetExpires' : null,
'passwordResetToken' : null
}, {new: true})
.then(user => {
res.send(user);
})
.catch(err => next(err))


This is my current Schema:



var userSchema = mongoose.Schema({
method: {
type: String,
enum: ['local', 'google', 'facebook']
},
local: {
email: {
type: String,
lowercase: true
},
password: String,
},
google: {
id: String,
email: {
type: String,
lowercase: true
},
name: String,
token: String
},
facebook: {
id: String,
name: String,
token: String
},
isVerified: {
type: Boolean,
default: false,
required: true
},
passwordResetToken: String,
passwordResetExpires: Date
});


I guess mongoose Is attempting to cast this hex string into a _id value? Is there some way to prevent mongoose from casting the string into an ObjectId Type?










share|improve this question


















  • 2





    That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

    – Neil Lunn
    Nov 26 '18 at 2:00













  • @NeilLunn of course... long day. Cheers

    – Bdyce
    Nov 26 '18 at 2:03
















0















I am attempting to findOneAndUpdatea string-based token on a User model. And
I receive the error:



Cast to ObjectId failed for value "{ passwordResetToken: '4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7',
passwordResetExpires: { '$gt': 1543196590882 } }" at path "_id" for model "User"


the document is stored like so:



{
"_id": {
"$oid": "5bfb424da0cc0923f05b67f1"
},
"local": {
"email": "XXXXXXXXXXXXXXXXX",
"password": "XXXXXXXXXXXXXXXXX"
},
"isVerified": false,
"method": "local",
"__v": 0,
"passwordResetExpires": {
"$date": "2018-11-26T02:41:17.851Z"
},
"passwordResetToken": "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"
}


and I query the document like so:



req.params.token = "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"


User.findByIdAndUpdate({
'passwordResetToken': req.params.token,
'passwordResetExpires': { $gt: Date.now() }
},
{
'local.password' : req.body.password,
'passwordResetExpires' : null,
'passwordResetToken' : null
}, {new: true})
.then(user => {
res.send(user);
})
.catch(err => next(err))


This is my current Schema:



var userSchema = mongoose.Schema({
method: {
type: String,
enum: ['local', 'google', 'facebook']
},
local: {
email: {
type: String,
lowercase: true
},
password: String,
},
google: {
id: String,
email: {
type: String,
lowercase: true
},
name: String,
token: String
},
facebook: {
id: String,
name: String,
token: String
},
isVerified: {
type: Boolean,
default: false,
required: true
},
passwordResetToken: String,
passwordResetExpires: Date
});


I guess mongoose Is attempting to cast this hex string into a _id value? Is there some way to prevent mongoose from casting the string into an ObjectId Type?










share|improve this question


















  • 2





    That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

    – Neil Lunn
    Nov 26 '18 at 2:00













  • @NeilLunn of course... long day. Cheers

    – Bdyce
    Nov 26 '18 at 2:03














0












0








0








I am attempting to findOneAndUpdatea string-based token on a User model. And
I receive the error:



Cast to ObjectId failed for value "{ passwordResetToken: '4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7',
passwordResetExpires: { '$gt': 1543196590882 } }" at path "_id" for model "User"


the document is stored like so:



{
"_id": {
"$oid": "5bfb424da0cc0923f05b67f1"
},
"local": {
"email": "XXXXXXXXXXXXXXXXX",
"password": "XXXXXXXXXXXXXXXXX"
},
"isVerified": false,
"method": "local",
"__v": 0,
"passwordResetExpires": {
"$date": "2018-11-26T02:41:17.851Z"
},
"passwordResetToken": "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"
}


and I query the document like so:



req.params.token = "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"


User.findByIdAndUpdate({
'passwordResetToken': req.params.token,
'passwordResetExpires': { $gt: Date.now() }
},
{
'local.password' : req.body.password,
'passwordResetExpires' : null,
'passwordResetToken' : null
}, {new: true})
.then(user => {
res.send(user);
})
.catch(err => next(err))


This is my current Schema:



var userSchema = mongoose.Schema({
method: {
type: String,
enum: ['local', 'google', 'facebook']
},
local: {
email: {
type: String,
lowercase: true
},
password: String,
},
google: {
id: String,
email: {
type: String,
lowercase: true
},
name: String,
token: String
},
facebook: {
id: String,
name: String,
token: String
},
isVerified: {
type: Boolean,
default: false,
required: true
},
passwordResetToken: String,
passwordResetExpires: Date
});


I guess mongoose Is attempting to cast this hex string into a _id value? Is there some way to prevent mongoose from casting the string into an ObjectId Type?










share|improve this question














I am attempting to findOneAndUpdatea string-based token on a User model. And
I receive the error:



Cast to ObjectId failed for value "{ passwordResetToken: '4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7',
passwordResetExpires: { '$gt': 1543196590882 } }" at path "_id" for model "User"


the document is stored like so:



{
"_id": {
"$oid": "5bfb424da0cc0923f05b67f1"
},
"local": {
"email": "XXXXXXXXXXXXXXXXX",
"password": "XXXXXXXXXXXXXXXXX"
},
"isVerified": false,
"method": "local",
"__v": 0,
"passwordResetExpires": {
"$date": "2018-11-26T02:41:17.851Z"
},
"passwordResetToken": "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"
}


and I query the document like so:



req.params.token = "4946d72f19b9649d3f306a0f5be59005c884ae453fc049c7"


User.findByIdAndUpdate({
'passwordResetToken': req.params.token,
'passwordResetExpires': { $gt: Date.now() }
},
{
'local.password' : req.body.password,
'passwordResetExpires' : null,
'passwordResetToken' : null
}, {new: true})
.then(user => {
res.send(user);
})
.catch(err => next(err))


This is my current Schema:



var userSchema = mongoose.Schema({
method: {
type: String,
enum: ['local', 'google', 'facebook']
},
local: {
email: {
type: String,
lowercase: true
},
password: String,
},
google: {
id: String,
email: {
type: String,
lowercase: true
},
name: String,
token: String
},
facebook: {
id: String,
name: String,
token: String
},
isVerified: {
type: Boolean,
default: false,
required: true
},
passwordResetToken: String,
passwordResetExpires: Date
});


I guess mongoose Is attempting to cast this hex string into a _id value? Is there some way to prevent mongoose from casting the string into an ObjectId Type?







node.js mongodb express mongoose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 1:58









BdyceBdyce

355




355








  • 2





    That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

    – Neil Lunn
    Nov 26 '18 at 2:00













  • @NeilLunn of course... long day. Cheers

    – Bdyce
    Nov 26 '18 at 2:03














  • 2





    That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

    – Neil Lunn
    Nov 26 '18 at 2:00













  • @NeilLunn of course... long day. Cheers

    – Bdyce
    Nov 26 '18 at 2:03








2




2





That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

– Neil Lunn
Nov 26 '18 at 2:00







That's findOneAndUpdate(). The findByIdAndUpdate() is specifically a shorhand form for { _id: idValue }. You cannot use it for any other form. Even if _id in your document is anything other than ObjectId ( which is not the case here ), then you use the other method anyway.

– Neil Lunn
Nov 26 '18 at 2:00















@NeilLunn of course... long day. Cheers

– Bdyce
Nov 26 '18 at 2:03





@NeilLunn of course... long day. Cheers

– Bdyce
Nov 26 '18 at 2:03












1 Answer
1






active

oldest

votes


















1














In mongoose, if you use findByIdAndUpdate(), you have to provide a value that is the objectID. So, in your case, it tries to find an Object ID but cannot and hence you get an error. Something more appropriate to your use case would be findOneAndUpdate(). Here, you are free to use other parameters.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473863%2fmongoose-5-3-4-prevent-cast-to-objectid-for-query-with-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In mongoose, if you use findByIdAndUpdate(), you have to provide a value that is the objectID. So, in your case, it tries to find an Object ID but cannot and hence you get an error. Something more appropriate to your use case would be findOneAndUpdate(). Here, you are free to use other parameters.






    share|improve this answer




























      1














      In mongoose, if you use findByIdAndUpdate(), you have to provide a value that is the objectID. So, in your case, it tries to find an Object ID but cannot and hence you get an error. Something more appropriate to your use case would be findOneAndUpdate(). Here, you are free to use other parameters.






      share|improve this answer


























        1












        1








        1







        In mongoose, if you use findByIdAndUpdate(), you have to provide a value that is the objectID. So, in your case, it tries to find an Object ID but cannot and hence you get an error. Something more appropriate to your use case would be findOneAndUpdate(). Here, you are free to use other parameters.






        share|improve this answer













        In mongoose, if you use findByIdAndUpdate(), you have to provide a value that is the objectID. So, in your case, it tries to find an Object ID but cannot and hence you get an error. Something more appropriate to your use case would be findOneAndUpdate(). Here, you are free to use other parameters.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 2:55









        Rohan DharRohan Dhar

        1,1451418




        1,1451418
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473863%2fmongoose-5-3-4-prevent-cast-to-objectid-for-query-with-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen