Click here to Skip to main content
15,301,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I am currently working on a project where I would like users to be able to connect via google and save their data in my database (MongoDB). the redirection works perfectly but when I connect with my account I get an error like:
Cannot read property '0' of undefined
.

What I have tried:

file: google.strategy.ts

TypeScript
public async validate(accessToken: string, refreshToken:string, profile: any, done: VerifyCallback): Promise<User> {
       // const { name, emails, photos } = profile
       // const user = {
       //   email: emails[0].value,
       //   firstName: name.givenName,
       //   lastName: name.familyName,
       //   picture: photos[0].value,
       //   accessToken
       // }
       // done(null, user);
       const newUser = new this.UserModel();

       newUser.userId = uuidv4();
       newUser.accessToken = accessToken;
       newUser.name = profile.name.giveName + ' ' + profile.name.familyName;
       newUser.email = profile.emails[0].value;
       newUser.phone = profile.phone[0].value;
       newUser.picture = profile.photos[0].value;
       // done(null, newUser);

      return newUser.save();
   }
Posted
Updated 6-Oct-21 3:32am
Comments
Richard MacCutchan 6-Oct-21 8:49am
   
You need to identify exactly which line causes the error. Also I notice that you have misspelled givenName on line 16 above.
Karim Kompissi 6-Oct-21 9:00am
   
ok the givenName has been modified. the error comes from the fact that I do not return the done(null, newUser) as you see commented. so that the data can enter in the BD they must save by making a return done(null,NewUser)
Richard MacCutchan 6-Oct-21 9:02am
   
Sorry, I do not understand what you mean. And you have still not explained which line of code gives the error.

1 solution

Thank you, I succeeded.

my code.
TypeScript
public async validate(accessToken: string, refreshToken:string, profile: any, done: VerifyCallback): Promise<any> {
      const newUser = new this.UserModel();

      newUser.userId = uuidv4();
      newUser.accessToken = accessToken;
      newUser.name = profile.name.givenName + ' ' + profile.name.familyName;
      newUser.email = profile.emails[0].value;
      newUser.picture = profile.photos[0].value;

      return newUser.save(function(err) {
          if(err) throw err;

          return done(null, newUser);
      });
  }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900