liron
liron

Reputation: 87

angular2 json2typescript - jsonConvert.deserializeObject does not work

hello i am new in angular 2 and i am pulling from google , facebook and linkedin the data of user. i am trying to do deserialize object to instance that i created called UserLogin but it doesn't work.

signIn Method :

signIn(provider: string) {
    this.sub = this._auth.login(provider).subscribe(
        (loginDataObj) => {
            console.log(loginDataObj);
            let jsonConvert: JsonConvert = new JsonConvert();
            let user: UserLogin = jsonConvert.deserializeObject(loginDataObj, UserLogin);

            console.log(user.email);
            //user data 
            //name, image, uid, provider, uid, email, token (accessToken for Facebook & google, no token for linkedIn), idToken(only for google) 
        }
    )
}

UserLogin class:

    @JsonObject
export class UserLogins implements IUserLogin{

    @JsonProperty("email", String)
    public email: String;
    @JsonProperty("idToken", String)
    public idToken: String;
    @JsonProperty("image", String)
    public image: String;
    @JsonProperty("name", String)
    public name: String;
    @JsonProperty("provider", String)
    public provider: String;
    @JsonProperty("token", String)
    public token: String;
    @JsonProperty("uid", String)
    public uid: string;

}

when i do console.log to the data its printed ok but when i am trying to deserialize and printed the user.email its undefined

thanks for the helpers.

Upvotes: 0

Views: 1082

Answers (1)

Jason Griebeler
Jason Griebeler

Reputation: 61

Each variable needs to be initialized. I just found this out myself. @JsonProperty("email", String) public email: String = "";

Upvotes: 3

Related Questions