当我尝试通过express.js和OAuth lib保存并检查用户信息时,出现了这个问题。
该代码在另一个应用上运行良好,没有任何错误,我复制了代码并进行了一些更改(mlab数据库URI,google-secret,clientID)以适应新应用
我知道了....我很困惑,请帮助
TypeError: User.findOne is not a function
at Strategy.passport.use.GoogleStrategy [as _verify] (C:\Users\breezy\Desktop\New folder\website\config\passport-setup.js:25:6)
at C:\Users\breezy\Desktop\New folder\website\node_modules\passport-oauth2\lib\strategy.js:193:24
at C:\Users\breezy\Desktop\New folder\website\node_modules\passport-google-oauth20\lib\strategy.js:122:5
at passBackControl (C:\Users\breezy\Desktop\New folder\website\node_modules\oauth\lib\oauth2.js:134:9)
at IncomingMessage.<anonymous> (C:\Users\breezy\Desktop\New folder\website\node_modules\oauth\lib\oauth2.js:157:7)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
这些是我的文件...
// this is passport-setup.js File
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('../authentication/keys');
const User = require('../models/models');
passport.serializeUser((user, done)=>{
done(null, user.id);
});
passport.deserializeUser((id, done)=>{
User.findById(id).then((user)=>{
done(null, user);
});
});
passport.use(
new GoogleStrategy({
//options for GoogleStrategy
callbackURL:'/auth/google/redirect',
clientID:keys.google.clientID,
clientSecret:keys.google.clientSecret
},(accessToken,refreshToken,profile,done)=>{
//check if user already exists in our db
console.log(profile);
User.findOne({googleId:profile.id}).then((currentUser)=>{
if(currentUser){
//already have user
console.log('user is:' + currentUser);
done(null,currentUser);
}else{
//if not create new user on our db
new User ({
username:profile.displayName,
googleId:profile.id,
thumbnail:profile._json.image.url
}).save().then((newUser)=>{
console.log('new user created' + newUser);
done(null,newUser);
});
}
});
})
);
keys.js文件
// this is authentication/keys file
let dbKeys = {
dbURI: 'mongodb://xxxxxxxxxxxxxxxxxx',
// a local database for developing and testing.
local: 'mongodb://127.0.0.1:27017/scriptHouse'
}
// exporting the database info outside
module.exports = {
database: dbKeys,
google:{
clientID:'xxxxxxxxxxxx',
clientSecret:'xxxxx'
},
mongodb:{
dbURI:'mongodb://xxxxxxxx'
},
session: {
cookieKey:'xxxxx'
}
};
models.js文件
//this is models/models file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
username:String,
googleId:String,
thumbnail:String
});
const User = mongoose.model('user',userSchema);
module.exports = User;
请帮帮我....