【发布时间】:2020-07-12 11:23:38
【问题描述】:
我正在尝试在 mongoDB 表中创建一个项目,其中包含来自使用 passport-google-oauth20 登录到我的应用的 google 用户的个人资料中的“图片”URL。。 p>
以下是我的架构:
const userSchema = new mongoose.Schema ({
name: String,
picture: String,
googleId: String,
});
以下是google的攻略:
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "https://localhost:3000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
console.log(profile._json.picture);
const customerEmail = profile._json.email;
User.findOrCreate(
{ googleId: profile.id },
{ name: profile.displayName },
{ picture: profile._json.picture },
function (err, user) {
return cb(err, user);
});
}
));
在我的 mongo 表中,它将为“googleId”和“name”创建项目,但不会为“picture”创建项目。并且没有错误消息。
但有趣的是我可以console.log(profile._json.picture); 并且它确实记录了字符串(这是一个 URL - 不确定它的 URL 是否与此有关??)
知道我在这里做错了什么吗?我的假设是它在 MongoDB 方面是一个问题,因为在 console.log 中返回了 URL 字符串。
【问题讨论】:
-
您好!我猜你正在使用 mongoose findOrCreate 插件,这个问题的根本原因是你如何使用
User.findOrCreate()方法,你错误地传递了参数。我回答了这样一个问题here,看看它是否解决了问题。 -
@Tunmee 是的,成功了!非常感谢!
-
您可以为该答案投票,以便其他面临类似问题的人更容易看到它。
标签: node.js mongodb passport.js google-oauth