Search code examples
node.jsexpressmongoosepassport-localpassport-facebook

Mongoose schematype.castForQuery error. User authentication failed


I have a problem while using passport strategy using local-passport using node js and mongoose. Sometimes(not always) it shows the following error to which I could not figure out and I could not login to the system. I have used angular JS for client side.

TypeError: schematype.castForQuery is not a function
at cast (/Users/Harry/Desktop/socketio/node_modules/mongoose/lib/cast.js:229:32)
at Query.cast (/Users/Harry/Desktop/socketio/node_modules/mongoose/lib/query.js:2753:12)
at Query.findOne (/Users/Harry/Desktop/socketio/node_modules/mongoose/lib/query.js:1353:10)
at Function.findOne (/Users/Harry/Desktop/socketio/node_modules/mongoose/lib/model.js:1343:13)
at Strategy._verify (/Users/Harry/Desktop/socketio/app_api/config/passport.js:17:8)
at Strategy.authenticate (/Users/Harry/Desktop/socketio/node_modules/passport-local/lib/strategy.js:90:12)
at attempt (/Users/Harry/Desktop/socketio/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/Users/Harry/Desktop/socketio/node_modules/passport/lib/middleware/authenticate.js:349:7)
at module.exports.login (/Users/Harry/Desktop/socketio/app_api/controllers/authentication.js:302:4)
at Layer.handle [as handle_request] (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/layer.js:95:5)
at /Users/Harry/Desktop/socketio/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/index.js:330:12)
at next (/Users/Harry/Desktop/socketio/node_modules/express/lib/router/index.js:271:10)

Here is my code for which the error's pointing: I tried to login, but it says the above errors sometimes(not always).I could not find the errors.

module.exports.login = function(req, res) {
if (!req.body.email || !req.body.password) {
    sendJSONresponse(res, 400, {
        "message": "All fields Required."
    });
    return;
}

passport.authenticate('local', function(err, user, info) {
    var token;
    if (err) {

        sendJSONresponse(res, 400, err);
        return;
    }
    // console.log('user detail : '+user);
    if (user && user.verified === true) {
        token = user.generateJwt();
        sendJSONresponse(res, 200, {
            "token": token
        });

    } else {
        sendJSONresponse(res, 401, info);

    }

})(req, res); // make sure that req, res are available to the passport

};

Here is code for passport strategy :

passport.use(new LocalStrategy({
    usernameField: 'email'
},
function(username, password, done) {
    User.findOne({
        email: username
    }, function(err, user) {
        if (err) {
            console.log("passport_error : "+err);
            return done(err);
        }
        if (!user) {
            return done(null, false, {
                "message": "Incorrect Username."
            });
        }
        if (!user.validPassword(password)) {
            return done(null, false, {
                "message": "Incorrect Password." + password
            });
        }
        console.log(user);
        return done(null, user);

    });
}
));

Solution

  • I found the solution to my question. Now it works. I had used the following code using Object.prototype....

        Object.prototype.getKey = function(value) {
            for (var key in this) {
                if (this[key] == value) {
                    return key;
                }
            }
            return null;
        };
        var key = usernames.getKey(toUser);
    

    It successfully returned the key of certain value (toUser in my case). However, everytime I logout of the system, it was not working and was not logging back in. Now I removed the entire method and replace with following code...

        function findKey(user) {
            for (var key in usernames) {
                if (usernames[key] == user) return key;
            }
            return false;
        }
        var key= findKey(toUser);
    

    Which is simpler way to return key of cetrain value in JSON object. And Now, there is not a problem in my mongodb findOne method. Still couldnot figured out the reason behind the errors by using Object.prototype method to return key. However the strange error is gone with small changes.

    SO, if you guys were stuck in same error type (Schematype.castForQuery is not a function ), it might be helpful

    Thank you for your response though.