1

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!

2
  • if (applicationUser == null) { applicationUser.userId = sub; seems suspicious Commented May 31, 2017 at 11:39
  • 1
    if applicationUser == null is true how would it contain a userId ? For starters i think this should be if (applicationUser !== null) Commented May 31, 2017 at 11:46

2 Answers 2

4

There are at least three issues with your code.

First you disregard the asynchronous nature of your code, second you try to write to a property of an object that is null and third you pass the error callback as the success callback to your post.

I don't know what 'sub' is but you seem to use it as userId so I do as well. Here is a replacement suggestion that does what you are trying to do. Instead of chaining the logic in promises I use the async/await construct.

public async alreadyExists(sub: string) {
  try {
    let applicationUser: ApplicationUser = 
      await this.applicationUserService.getApplicationUser(sub).toPromise();

    if (applicationUser === null) {
      applicationUser = new ApplicationUser(sub);

      await this.applicationUserService.postApplicationUser(applicationUser)
       .toPromise();
    }
    localStorage.setItem('userId', sub);
  } catch (error) {
    this.errorMessage = error;
  }
}

I hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

I think localStorage should be set inside in one of the async callbacks (OPs intention), +1
Perhaps you are right but I'll leave it as OP has it as it is not strictly broken code.
2
let applicationUser: ApplicationUser;

The above line will not create the actual object instance here, its just a reference. Error says applicationUser is undefined

You can try doing

let applicationUser: ApplicationUser = new ApplicationUser ('', '');

Alternatively (not tested)

let applicationUser: ApplicationUser
 applicationUser = Object.create(ApplicationUser.prototype);
 this.constructor.apply(applicationUser, ['', '']);

2 Comments

@GSeriousB I don't think it will work the way you intended it to be, Hampus's answer states all your misthoughts
I'll second what @echonax said. Although it will no longer have runtime crashes it will not work as you intend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.