I want to parse a firebase auth credentials object which is differently formatted for various sign in methods.
For example for password sign up the data I want are in credentials directly, but for facebook or google login the data I want are in credentials.user
So I have this function going:
parseCredentials(credentials: any): User {
const parsedCredentials: User = {
displayName: credentials.displayName || credentials.user.displayName,
email: credentials.email || credentials.user.email,
emailVerified: credentials.emailVerified || credentials.user.emailVerified,
uid: credentials.uid || credentials.user.uid
};
return parsedCredentials;
}
As you can see I use the function to return a new User object based on the credentials passed to the fn, because I don't need unnecessary information in the User object.
The problem is when I sing up with email-password I get error: credentials.user is undefined
I think is something wrong with the || operators. It seems that credentials.user is undefined (because the user property does not exist on credentials object when signing up with email-password).
But thats why I used the || operator. Struggling with this for a while but I can't figure out. Hope you can give some help. Thanks.
Your approach is correct.
In each case, what's on the right-hand side of the ||
is not evaluated if what's on the left is not undefined
.
Most likely the error is due to credentials.displayName
being undefined
, which means that the program will try to evaluate credentials.user.displayName
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments