Tuesday 14 March 2017

It appears that mongoose is trying to cast FaceBook id as mongodb object id

I will show you the error:

2017-03-14T22:03:20.176413+00:00 app[web.1]: CastError: Cast to ObjectId failed for value "10100701469924880" at path "_id" for model "User" 2017-03-14T22:03:20.176417+00:00 app[web.1]: at MongooseError.CastError (/app/node_modules/mongoose/lib/error/cast.js:26:11) 2017-03-14T22:03:20.176418+00:00 app[web.1]: at ObjectId.cast (/app/node_modules/mongoose/lib/schema/objectid.js:147:13) 2017-03-14T22:03:20.176419+00:00 app[web.1]: at ObjectId.castForQuery (/app/node_modules/mongoose/lib/schema/objectid.js:187:15) 2017-03-14T22:03:20.176420+00:00 app[web.1]: at cast (/app/node_modules/mongoose/lib/cast.js:229:32) 2017-03-14T22:03:20.176421+00:00 app[web.1]: at Query.cast (/app/node_modules/mongoose/lib/query.js:2987:12) 2017-03-14T22:03:20.176422+00:00 app[web.1]: at Query.findOne (/app/node_modules/mongoose/lib/query.js:1393:10) 2017-03-14T22:03:20.176422+00:00 app[web.1]: at Function.findOne (/app/node_modules/mongoose/lib/model.js:1304:13) 2017-03-14T22:03:20.176423+00:00 app[web.1]: at Function.findById (/app/node_modules/mongoose/lib/model.js:1232:15) 2017-03-14T22:03:20.176424+00:00 app[web.1]: at /app/DB/passportSetUp.js:151:10

10100701469924880 is the id from FB so I don't know why im getting the error

line 151 in /app/DB/passportSetUp.js is where I have do serializing (you could see)

Here is the serialize stuff:

passport.serializeUser(function(user, done){
    done(null, user._id);
});

passport.deserializeUser(function(_id, done){
    User.findById(_id, function(err, user){
        done(err,user);
    });
});

routes :

app.get("/login/facebook", passport.authenticate("facebook", {scope: ['email']} ));
app.get("/login/facebook/return",
    passport.authenticate("facebook", {failureRedirect:"/login"}),
    function(req, res){
        res.redirect("/")
    }
)

Strategy:

passport.use(new FBstrategy({
    clientID : FB.appId,
    clientSecret : FB.appSecret,
    callbackURL : FB.redirect,
    profileFields: ['emails','id', 'first_name', 'gender', 'last_name', 'picture', "link", "verified", "work"]
}, function(accessToken, refreshToken, profile, cb){
    console.log(">>>Hit in FACEBOOK<<<<")
    console.log("<PROFILE >", profile);
    console.log("profile._json > ", profile._json)
    console.log("profile.first_name : ",profile.first_name)

    fbObject = {
        usingFb : true,
        fbId : profile.id,
        accessToken : accessToken,
        photos : profile.photos,
        username : profile.name.givenName,
        profileImg : profile.profileUrl

    }
    new User({facebook : fbObject}).save()
        .then(fbUser =>{
            console.log("FBUSER SAVED", fbUser)
        })
        .catch(err => {console.log("FB" , err)})
    return cb(null, profile)
}))

I do get consoled logged "hit in facebook" .it does seem that I get profile info. It's interesting that in my schema I have usingFb : {type : Boolean, default : false}, but it doesn't change to true when doc is saved I try to do that when I save in the strategy (usingFb : true,).

the page gets redirected to

https://authup.herokuapp.com/login/facebook/return?code=AQAwLBI7rkjie13fs1Owh1_7bC2m4nl6JpoJU36l8Vinf2QlDk_aGSLCX7GpYuai4U_V7JQ7a8FWDuz1XO5_CyBpOr6GstWOYTNT2FgFra6nZaIbgPKzzTkXB8NsgXJnvmLpgVLhxtp90C-8lxm7k73_uLMSatd3ps4dxXh6A_b21xZkEbPzZ7_vpFiGOrmd1uiwpxsJecCeEgKsxcAieNcJoKxaavD5lFsX4kjvb_0hRa57LYFpxgwRqS0Sq0ZvL9RCmK_6pLHqPSbgSC1HOwgrQGUYPJj98esutRupcGiBUfa1aXUJlNY0-pikZIale2s#_=_

and an "Error" in the title. I hope that will go away once this Object cast error goes away

Scehma:

const userSchema = new Schema({
    local : {
        username : String,
        password : String,
        email : String,
        dateCreated : { type: Date, default: Date.now },
        profileImg : {type : String , default : "man1.jpg"},
        profileQs : Array,
        bio : String,
        location : String,
        repPoints : [repPointsScehma],
        reps : Number, //virtual
    },
    facebook : {
        usingFb : {type : Boolean, default : false},
        provider : String,
        photos : Array,
        profileUrl : String,
        gender : String,
        name : Schema.Types.Mixed,
        fbId : String,
        accessToken :String,
        username : String,
        email : String,
        dateCreated : { type: Date, default: Date.now },
        profileImg : String,
        profileQs : Array,
        bio : String,
        location : String,
        repPoints : [repPointsScehma],
        reps : Number, //virtual
    }

}, {
    toObject :{
        virtuals : true
    },
    toJSON : {
        virtuals : true
    }
});

userSchema.virtual("repPointsToDisplay").get(function(){
    if(this.repPoints){
        return repPointAdder(this.repPoints)
    }

})



via jack blank

No comments:

Post a Comment