猫鼬保存错误LinkedIn数据

时间:2018-07-16 23:24:18

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

我使用Passport-LinkedIn进行身份验证并获取一些用户数据,但是Passport-LinkedIn返回一些定义的数据,例如电子邮件,格式化名称(名字+姓氏)等以及您在Strategy中定义的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'

谢谢!

1 个答案:

答案 0 :(得分:0)

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

//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);

          }
      });
  }
));