I created a model and in a method when I want to give values the the model's properties, I get this error: TypeError: Cannot set property 'userId' of undefined
Here is my Model:
export class ApplicationUser {
constructor(
public id: string,
public userId: string
) { }
}
Here is the method:
public alreadyExists(sub: string) {
let applicationUser: ApplicationUser;
this.applicationUserService.getApplicationUser(sub)
.subscribe(
appUser => applicationUser = appUser,
error => this.errorMessage = <any>error
);
if (applicationUser == null) {
applicationUser.userId = sub;
this.applicationUserService.postApplicationUser(applicationUser)
.subscribe(
error => this.errorMessage = <any>error
);
}
localStorage.setItem('userId', sub);
}
The error appears after this line: applicationUser.userId = sub;
What do I have to change/add to don't get this error message?
Thank You!
if (applicationUser == null) { applicationUser.userId = sub;seems suspiciousapplicationUser == nullis true how would it contain a userId ? For starters i think this should beif (applicationUser !== null)