What is function User.findOrCreate doing and when is it called in passport?

前端 未结 2 1377
陌清茗
陌清茗 2020-12-07 12:30

I can\'t find documentation on this function and therefor I can\'t make it work right. When is that function being called, what is it doing and what is it taking as first pa

2条回答
  •  醉酒成梦
    2020-12-07 13:18

    If you would like to use findOrCreate, try the npm package mongoose-findorcreate, or supergoose

    e.g. mongoose-findorcreate

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost');
    
    var findOrCreate = require('mongoose-findorcreate')
    var Schema = mongoose.Schema;
    var UserSchema = new Schema({ facebookId: Number});
    UserSchema.plugin(findOrCreate);
    var User = mongoose.model('User', UserSchema);
    
    passport.use(new FacebookStrategy({
            clientID: 'clientID',
            clientSecret: 'clientSecret',
            callbackURL: "/auth/facebook/callback"
        },
        function(accessToken, refreshToken, profile, cb) {
            User.findOrCreate({ facebookId: profile.id }, function (err, user) {
              console.log('A new uxer from "%s" was inserted', user.facebookId);
              return cb(err, user);
            });
        }
    ));
    

提交回复
热议问题