Assigning one of many values to object's property

Rafff

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.

bugs

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.

edited at
0

Comments

0 comments
Login to comment

Related

Function used for assigning something as an object's property?

Convert JSON with a ton of values under each (property? object?) to CSV with many rows is returning all values in one string

Assigning one property to another inside jQuery Object Literal

What's the meaning of assigning one variable with 2 values in Java

Copying selected property values of one object to another

assigning values to a computed property vuejs

Assigning Retained Object to Unsafe Property

Assigning retained object to weak property

List of dictionaries to list of one property's values

How to get object from many to one relation, by some property

Get object type based on it's property values

Object's float property returns inconsistent values

Assigning values to object fields in Java?

Assigning values to an object inside a promise

assigning FormGroup object values in angular

Having trouble with adding one property to an object inside a list after assigning the rest of them

laravel 5.8 access other table's property in one to many relation

Laravel Eloquent one --> many relationship assigning parent

how to add many values to one key in javascript object

Assigning one object's fields from another before nulling the first object

Convert array of objects with same property to one object with array values

How to combine values of an object property into one based on a condition- Javascript

object property nil despite assigning him value

"Assigning retained object to weak property" with init?

Assigning new property to object using deconstruction

Assigning an executable function to a property of a Javascript object

Typescript "Object is possibly undefined" while assigning property

What are they doing in this piece of code (Assigning $({}) to an object property)?

How to assign an object's property to another one of it's properties in reactjs?