0

I have two methods in my service which do very simillar things, so the subscribtion is the same. I wanted to join exection so when firing my service method

this.createNewUser(this.userModel, this.userDefaultGroupSelectedId)
            .subscribe(
            res => {
                this.msgs = ({ severity: "success", summary: "Success:\n", detail: res });
                this.userModel = new User();
                this.onConfirm.emit();
            },
            error => {
                this.msgs = ({ severity: "error", summary: "Error Message:", detail: error.Message ? error.Message : error.toString() });
            }

it would call createNewUser which shold return which function should be called

createNewUser(user: IUser, userDefaultGroupSelectedId: number): Observable<string> 
    {
        if (this.asRegistration == false) {
            return this.userAdministrationService.addNewUser(this.userModel, this.userDefaultGroupSelectedId);
        }
        else {
            this.userAdministrationService.registerNewUser(this.userModel);
        }
    }

but this approach does not work, I got Cannot read property 'subscribe' of undefined

my services:

addNewUser(user: IUser, userDefaultGroupSelectedId: number): Observable<string> {

    return this.http.post(this.addNewUserUrl)
        .catch(error => super.handleError(error))
        .map(response => response.json());
}

registerNewUser(user: IUser): Observable<string> 
{

    return this.http.post(this.registerNewUserUrl)
        .catch(error => super.handleError(error))
        .map(response => response.json());
}
4
  • apparently addNewUser or registerNewUser function returns undefined. Commented Oct 8, 2017 at 21:14
  • Show us your addNewUser and registerNewUser function implementations Commented Oct 8, 2017 at 21:18
  • @IgorSoloydenko updated Commented Oct 9, 2017 at 16:36
  • Actually, @meligy has already answered your question. Somehow, the return keyword got lost in the else-clause before the function invocation. Commented Oct 9, 2017 at 16:48

1 Answer 1

3

The problem seems to be that you are not returning the result of this.userAdministrationService.registerNewUser(this.userModel);, so the return of the function in this case is undefined.

Try changing it to:

return this.userAdministrationService.registerNewUser(this.userModel);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.