Node.js User.findOrCreate不是一个函数
我遵循了的github指南,这是我得到的结果: User.findOrCreate不是一个函数 我的passport.js中有以下代码:Node.js User.findOrCreate不是一个函数,node.js,mongoose,passport-facebook,Node.js,Mongoose,Passport Facebook,我遵循了的github指南,这是我得到的结果: User.findOrCreate不是一个函数 我的passport.js中有以下代码: passport.use(new FacebookStrategy({ clientID : configAuth.facebookAuth.clientID, clientSecret : configAuth.facebookAuth.clientSecret, callbackURL : configA
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);
是,创建您自己的findOrCreate方法,其中包含一个用户。findOne{facebookID:profile.id},如果findOne没有结果,则回调用户。创建{facebookID:profile.id,username:profile.displayName…}。
你应该在你的模式中添加facebookID,否则你将为每个facebook连接创建一个用户。这就是你如何在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);
}
});
};
通过使用npm安装mongoose findorcreate软件包来使用该功能。
npm安装mongoose findorcreate
在代码中声明它。
const findOrCreate=requiremongose findOrCreate
创建模式时,将函数添加为插件以使用它。
yourSchema.pluginindorcreate
中提琴!完成了