【问题标题】:User.findOrCreate is not a function passport-facebookUser.findOrCreate 不是函数 passport-facebook
【发布时间】:2016-02-14 09:49:43
【问题描述】:

我遵循了passport-facebook 的 github 指南,这是我得到的结果:

User.findOrCreate 不是函数

我的 passport.js 中有此代码:

passport.use(new FacebookStrategy({
    clientID        : configAuth.facebookAuth.clientID,
    clientSecret    : configAuth.facebookAuth.clientSecret,
    callbackURL     : configAuth.facebookAuth.callbackURL
    },
    function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ facebookId: profile.id }, function (err, user) {
    return done(err, user);
    });
    }
));

我需要自己编写 findOrCreate 方法吗?我以为猫鼬正在处理这个?

另外,profile.id 有什么作用?我是否需要将其编辑为与我的代码相关的内容?我没有名为 profile 的模型。

这是我的用户模型:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var crypto = require('crypto');
var jwt = require('jsonwebtoken');

var UserSchema = new mongoose.Schema({
    username: { type: String, lowercase: true, unique: true },
    firstname: { type: String},
    lastname: { type: String},
    difficulty: { type: String},
    isstudent: { type: Boolean },
    haschildren: { type: Boolean},
    gender: { type: String },
    email: { type: String, unique: true},
    birthdate: String,
    isdoingchallenges: { type: Boolean },
    challengescompleted: [{ type: ObjectId, ref: 'Challenge' }],
    currentchallenge: { type: ObjectId, ref: 'Challenge' },
    challengessuggestions: [{ type: ObjectId, ref: 'Challenge' }],
    isfacebooklogin:{type:Boolean},
    facebookid:{type:String},
    hash: String,
    salt: String
});

UserSchema.methods.setPassword = function(password){
    this.salt = crypto.randomBytes(16).toString('hex');
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};

UserSchema.methods.validPassword = function(password) {
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
};

UserSchema.methods.generateJWT = function() {

    // set expiration to 60 days
    var today = new Date();
    var exp = new Date(today);
    exp.setDate(today.getDate() + 60);

    return jwt.sign({
        _id: this._id,
        username: this.username,
        exp: parseInt(exp.getTime() / 1000),
    }, 'SECRET');
};

mongoose.model('AppUser', UserSchema);

【问题讨论】:

    标签: node.js mongoose passport-facebook


    【解决方案1】:
    1. 通过 npm 安装 mongoose-findorcreate 包来使用该功能。

    npm install mongoose-findorcreate

    1. 在您的代码中声明它。

    const findOrCreate = require("mongoose-findorcreate");

    1. 创建架构时,将函数添加为插件以使用它。

    yourSchema.plugin(findOrCreate);

    维奥拉!完成了!

    【讨论】:

      【解决方案2】:

      这是您可以在 mongoose 架构/模型上添加静态方法的方法

      userSchema.statics.findOrCreate = function findOrCreate(profile, cb){
          var userObj = new this();
          this.findOne({_id : profile.id},function(err,result){ 
              if(!result){
                  userObj.username = profile.displayName;
                  //....
                  userObj.save(cb);
              }else{
                  cb(err,result);
              }
          });
      };
      

      【讨论】:

        【解决方案3】:

        是的,请创建您自己的包含 User.findOne({facebookID:profile.id},callback) 的 findOrCreate 方法,然后如果 findOne 没有结果 User.Create({facebookID : profile.id , username : profile.displayName .. .}) . 您应该在您的架构上添加 facebookID,否则您将为每个 facebook 连接创建一个用户。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-11
          • 2017-04-18
          • 2019-08-08
          • 2020-05-18
          • 1970-01-01
          • 2017-11-26
          • 1970-01-01
          • 2016-05-06
          相关资源
          最近更新 更多