0

I have my service signUp

async signUp(credentials: ICreateCredentials) {
    try {
      const user = await Auth.signUp({
        username : credentials.email,
        password: credentials.password,
        attributes: {
          email: credentials.email,
          phone_number: ''
        }
      });

      return user;
    } catch (error) {
      console.log(" sign up error ");
      throw error;
    }
  }

This service is called in my register.component.ts file

  async onSubmit() {
    if (!this.registerForm.valid || this.buttonDisabled) {
      return;
    }
    this.buttonDisabled = true;
    this.buttonState = 'show-spinner';

    try {
      const user = this.authService.signUp(this.registerForm.value);
      console.log({ user });
    } catch (error) {
      console.log(' ooook');
      // The registration failed
      this.notifications.create(
        'Error',
        error.message,
        NotificationType.Bare,
        { theClass: 'outline primary', timeOut: 6000, showProgressBar: false }
      );
      this.buttonDisabled = false;
      this.buttonState = '';
    }  

  }

When Auth.signUp function raise an error, i can see "sign up error" then the stack trace in my browser, I never enter inside the catch of submit to handle it correctly in a pop up.

I get the message error in my browser:

ERROR Error: "Uncaught (in promise): Object: {"code":"UsernameExistsException","name":"UsernameExistsException", "message":"An account with the given email already exists."}"

What is wrong with my code ? Thanks in advance.

7
  • Is your service return observable? Commented May 14, 2020 at 21:17
  • @AdamNorman no, because I use async, the service return a promise, but according to the documentation, I can use try/catch Commented May 14, 2020 at 21:47
  • could you strongly type the expected return of you function async signUp(credentials: ICreateCredentials) : Promise<T>{ Commented May 14, 2020 at 21:56
  • When i strongly type as suggested I get the error : error TS2304: Cannot find name 'T'. Commented May 14, 2020 at 21:59
  • replace T with the User Type or set It To any Commented May 14, 2020 at 22:09

0

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.