【问题标题】:Using passport-google-oauth20, trying to create item mongo for picture URL使用 passport-google-oauth20,尝试为图片 URL 创建项目 mongo
【发布时间】: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


【解决方案1】:

感谢@Tunmee 和他对此link 的回答。 我没有正确使用 findOrCreate 方法。我已将我的功能更改如下:

function(accessToken, refreshToken, profile, cb) {
    console.log(profile);
    var profilePicture = profile._json.picture;
    console.log(profilePicture + "inside function");
    const customerEmail = profile._json.email;
    User.findOrCreate(
      { googleId: profile.id, name: profile.displayName, picture: profile._json.picture  },
      function (err, user) {
      return cb(err, user);
    });

以前,我希望在 MongoDB 文档中使用的其他键和值作为不同的参数传递,而不仅仅是条件参数。

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 1970-01-01
    • 2018-07-04
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2011-01-29
    • 2020-12-14
    相关资源
    最近更新 更多