node.js - User.findOrCreate 不是函数 Passport-facebook

标签 node.js mongoose passport-facebook

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

User.findOrCreate is not a function

我的 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方法吗?我以为 Mongoose 正在处理这个?

另外,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);

最佳答案

  1. 通过 npm 安装 mongoose-findorcreate 包来使用该功能。

npm install mongoose-findorcreate

  • 在您的代码中声明它。
  • const findOrCreate = require("mongoose-findorcreate");

  • 创建架构时,将该函数添加为插件以使用它。
  • yourSchema.plugin(findOrCreate);

    中提琴!完成了!

    关于node.js - User.findOrCreate 不是函数 Passport-facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33682152/

    相关文章:

    node.js - 在 APIrequest Nodejs Express 上打开浏览器弹出窗口

    node.js - 子文件与 Mongoose 人口

    mongodb - 从 $elemMatch 查询返回数组索引

    javascript - 错误 : `useFindAndModify` is an invalid option

    https - 不安全登录被阻止 : You can't get an access token or log in to this app from an insecure page. 尝试将页面重新加载为 https://

    node.js - 在 Docker GitHub 操作中缓存 npm 包

    node.js - 按关联模型排序时,Sequelize 抛出错误 "Unable to find a valid association for model x"

    javascript - Express 正在提供 index.html 而不是我的 Webpack 包。我的配置文件有问题吗?

    node.js - 如果在 Passport-facebook Node js 的数据库中找不到用户,则重定向到注册页面

    node.js - 由于 "AuthorizationError",未捕获来自 Passport-facebook 的服务器错误