using passport module to get facebook profile events

319 views Asked by At

i am trying to get future events user attended to using passport module but im clearly missing something. i followed the Facebook API and got the permission i needed like so :

    router.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'public_profile', 'user_events' ] }));
    router.get('/auth/facebook/callback', passport.authenticate('facebook', {
        successRedirect:'/welcome',
        failureRedirect:'/'
    }))

but when i am trying to print an event like this:

 passport.use(new FacebookStrategy({
        clientID: config.fb.appID,
        clientSecret: config.fb.appSecret,
        callbackURL: config.fb.callbackURL,
        profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
    }, function(accessToken, refershToken, profile, done){
        //Check if the user exists in our Mongo DB database
        //if not, create one and return the profile
        //if exists, return profile
        userModel.findOne({'profileID':profile.id}, function(err, result){
            if(result){
                done(null,result);
            } else {
                // Create a new user in our mongoLab account
                var newFbUSer = new userModel({
                    profileID: profile.id,
                    fullname: profile.displayName,
                    profilePic:profile.photos[0].value || '',
                    birthday:profile.birthday,
                    events:profile.events[0].name,
                    profileUrl:profile.profileUrl,

                });
                console.log(newFbUSer.profilePic);


                newFbUSer.save(function(err){
                    done(null,newFbUSer);
                })
            }
        })

i get this error:

                    events:profile.events[0].name,
                                         ^
TypeError: Cannot read property '0' of undefined

i tried to replace name with value, as it is working perfectly fine with the photos, and i tried many other ways but i simply can't get this one to work...any help please?

0

There are 0 answers