我正在使用ExpressJS和PassportJS的Facebook策略尝试在认证回调上执行一些条件重定向,当某些属性丢失时应该调用它。
我的用户架构:
var UserSchema = new Schema({
id: ObjectId,
uid: String,
facebookToken: String,
username: String,
password: String,
salt: String,
firstName: String,
lastName: String,
email: String,
birthday: Date,
gender: String,
location: String,
phone: Number,
verifiedPhone: Boolean,
interests: {
culture: Boolean,
food: Boolean,
business: Boolean,
family: Boolean,
learning: Boolean,
sports: Boolean,
movies: Boolean,
music: Boolean,
events: Boolean,
nightlife: Boolean,
health: Boolean,
beauty: Boolean,
fashion: Boolean,
motoring: Boolean,
electronics: Boolean,
groceries: Boolean,
travel: Boolean,
decor: Boolean
},
weeklyNotifcation: Number,
weeklyNotificationSent: Number,
created: {type: Date, default: Date.now}
});
我的回调:
app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), function(req, res) {
User.findOne({uid: req.session.uid}, function(err, user) {
if(user.email || user.location || user.phone || user.location == undefined) {
res.redirect('/register');
} else {
res.redirect('/');
}
});
});
调用/auth/facebook
后,数据将存储到mongo中。我检查数据库是否反对使用会话并检查它是否未定义,因为它不会返回字段,因为它们没有存储在数据库中。回调失败并且不返回任何数据。
任何帮助将不胜感激。
答案 0 :(得分:1)
if(user.email === undefined || user.location === undefined || user.phone === undefined || user.location === undefined) {
可能就是你的意思。此外,您应该检查err
变量以查看是否存在错误。