【问题标题】:Mongoose Save Error LinkedIn DataMongoose 保存错误 LinkedIn 数据
【发布时间】:2018-07-16 23:24:17
【问题描述】:

我使用 Passport-LinkedIn 进行身份验证并获取一些用户数据,但 Passport-LinkedIn 返回一些定义的数据,如电子邮件、格式化名称(名字 + 姓氏)等以及您在策略中定义的 JSON 数据,我定义如下:

        profileFields: [
        'id',
        'headline',
        'summary',
        'first-name',
        'last-name',
        'email-address',
        'public-profile-url',
        'picture-urls::(original)',
        'industry']
} 

我定义了这样的函数:

    //PassportJs - LinkedIn
passport.use(new linkedInStrategy({
    consumerKey: config.consumerKey,
    consumerSecret: config.consumerSecret,
    callbackURL: config.callbackURL,
    scope: config.scope,
    profileFields: config.profileFields
  },
  function(token, tokenSecret, profile, done) {
      User.findOne({
          linkedinId: profile.id
      }, (err, user)=>{
          if(err){
              return done(err);
          }
          //No user was found, Create new
          if(!user){
              //Logs
              console.log(profile);

              user = new User({
                  linkedinId: profile.id,
                  firstName: profile._json.firstName,
                  lastName: profile._json.lastName,
                  formattedName: profile.displayName,
                  email: profile.emails[0].value,
                  linkedinUrl: profile._json.publicProfileUrl,
                  industry: profile._json.industry,
                  summary: profile._json.summary,
                  profileImage: profile._json.pictureUrl.value[0],
                  headline: profile._json.headline
              });
              user.save((err)=>{
                  if (err) console.log(err);
                  return done(err, user); // If !error return User and Save
              });
          } else{
              //found user. return
              console.log(profile);
              return done(err, user);

          }
      });
  }
));

但如果用户在定义的数据中没有个人资料图片或缺少字段,则会出现如下错误:

events.js:183
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'value' of undefined
    at User.findOne (/home/berkay-ubuntu/Projects/ecosystem-mail/auth/linkedin.js:51:58)
    at /home/berkay-ubuntu/Projects/ecosystem-mail/node_modules/mongoose/lib/model.js:4467:16
    at model.Query.Query._completeOne (/home/berkay-ubuntu/Projects/ecosystem-mail/node_modules/mongoose/lib/query.js:1704:12)
    at Immediate.Query.base.findOne.call (/home/berkay-ubuntu/Projects/ecosystem-mail/node_modules/mongoose/lib/query.js:1764:10)
    at Immediate._onImmediate (/home/berkay-ubuntu/Projects/ecosystem-mail/node_modules/mquery/lib/utils.js:119:16)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)

如果缺少数据,我想定义'null'

谢谢!

【问题讨论】:

    标签: node.js mongoose mongoose-schema passport-linkedin


    【解决方案1】:

    我后来找到了这样的解决方案:

    //PassportJs - LinkedIn
    passport.use(new linkedInStrategy({
        consumerKey: config.consumerKey,
        consumerSecret: config.consumerSecret,
        callbackURL: config.callbackURL,
        scope: config.scope,
        profileFields: config.profileFields
      },
      function(token, tokenSecret, profile, done) {
          User.findOne({
              linkedinId: profile.id
          }, (err, user)=>{
              if(err){
                  return done(err);
              }
              //No user was found, Create new
              if(!user){
                  //Logs
                  console.log(profile);
                  // console.log(profileImage);
                  profileImage = null;
                  if (!profile._json.pictureUrls){
                      console.log('Undefined');
                      console.log(this.profileImage);
                  }else{
                      console.log('Defined');
                      this.profileImage =  profile._json.pictureUrls.values[0];
                      console.log(this.profileImage);
                  }
                  user = new User({
                      linkedinId: profile.id,
                      firstName: profile._json.firstName,
                      lastName: profile._json.lastName,
                      formattedName: profile.displayName,
                      email: profile.emails[0].value,
                      linkedinUrl: profile._json.publicProfileUrl,
                      industry: profile._json.industry,
                      summary: profile._json.summary,
                      profileImage: this.profileImage,
                      headline: profile._json.headline
                  });
                  user.save((err)=>{
                      if (err) console.log(err);
                      return done(err, user); // If !error return User and Save
                  });
              } else{
                  //found user. return
                  console.log(profile);
                  return done(err, user);
    
              }
          });
      }
    ));
    

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2021-05-29
      • 2014-12-29
      • 2021-02-14
      相关资源
      最近更新 更多