我试图使用护照添加Facebook登录,并且我遇到了以下错误:
TypeError:无法调用方法' findOne'在Strategy._verify中未定义
我在app.js文件中需要 models.js ,并且我已经进入了护照模型
app.js
var Models = require('./config/models');
require('./config/passport')(passport,loginStrategies,Models);
我的护照型号 - passport.js :
module.exports = function(passport, loginStrategies, Models){
//Passport serialize and deserialize user
passport.use(new loginStrategies.FacebookStrategy({
//FB App Config
},function(token, tokenSecret, profile, done) {
Models.User.findOne({uid: profile.id}, function(err, user) {
if(user) {
done(null, user);
} else {
var user = new Models.User();
user.uid = profile.id;
user.name = profile.displayName;
user.email = profile.email;
user.save(function(err) {
if(err) {
throw err;
}
done(null, user);
});
}
});
// ...
Models.js
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
module.exports = function(){
DB_URL = "mongodb://localhost/test";
//User Schema
var UserSchema = new Schema({
uid: Number,
name: String,
email: String,
});
mongoose.connect(DB_URL);
mongoose.model('User', UserSchema);
return{
User : mongoose.model('User')
};
};
答案 0 :(得分:1)
由于models.js
正在导出function
:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
module.exports = function(){ // <!---
// ...
};
需要为Object
添加User
模型的return
来调用它。模块的位置require
d:
var Models = require('./config/models')();
// ^^
require('./config/passport')(passport,loginStrategies,Models);
console.log(typeof Models); // 'object'
console.log('User' in Models); // true
或者,它在哪里使用:
module.exports = function(passport, loginStrategies, Models){
//Passport serialize and deserialize user
var models = Models();
// ...
models.User.findOne(...);