Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose保存LinkedIn数据错误_Node.js_Mongoose_Mongoose Schema_Passport Linkedin - Fatal编程技术网

Node.js Mongoose保存LinkedIn数据错误

Node.js Mongoose保存LinkedIn数据错误,node.js,mongoose,mongoose-schema,passport-linkedin,Node.js,Mongoose,Mongoose Schema,Passport Linkedin,我使用Passport LinkedIn进行身份验证并获取一些用户数据,但Passport LinkedIn返回一些已定义的数据,如电子邮件、格式化名称(名字+姓氏)等,以及您在策略中定义的JSON数据,我定义如下: profileFields: [ 'id', 'headline', 'summary', 'first-name', 'last-name', 'email-addre

我使用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);
              // 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);

          }
      });
  }
));
我这样定义函数:

    //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”


谢谢大家!

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

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

          }
      });
  }
));