【发布时间】:2020-06-09 02:47:50
【问题描述】:
我正在使用 Node.js、MongoDB 和 Mongoose,并且正在使用 passport.js 进行身份验证。
这是我的用户架构:
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String,
facebookId: String,
profilePic: String,
fName: String,
lName: String
});
还有我的谷歌策略:
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/dashboard",
profileFields: ["id", "displayName", "photos", "email"]
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
console.log(profile.photos[0].value);
User.findOrCreate(
{ googleId: profile.id },
{ profilePic: profile.photos[0].value },
{ email: profile.emails[0].value },
function(err, user) {
return cb(err, user);
}
);
}
)
);
当我console.log 我的结果时,我看到了我的个人资料,以及个人资料照片网址和个人资料电子邮件,但我看不到我的电子邮件 ID。只创建了 4 个字段:
_idgoogleIdprofilePic_v
谁能告诉我如何保存电子邮件字段?
【问题讨论】:
-
我在mongoose文档中好像找不到
Model.findOrCreate方法,你用的是什么版本的mongoose? -
我正在使用一个单独的猫鼬 findOrCreate 插件,它将 findOrCreate 方法添加到模型中。这对于像 Passport 这样需要它的库很有用。您可以在此处找到更多详细信息:npmjs.com/package/mongoose-findorcreate
标签: node.js mongodb mongoose passport.js