1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cognito、Javascript SDKのエラー処理

Posted at

サンプルコードではエラー処理の仕方がわからなかった。
signUpに失敗する理由はいくつかあるが、理由に応じてエラーメッセージを表示する場合はどうすればよいか?

結論からいうと、userPool.signUpを呼ぶ際に指定するコールバック関数のerrオブジェクトのcode属性を見ればよい:

login.js
        userPool.signUp($("#email").val(), $("#password").val(), attributeList, null, function(err, result) {
          if(err){
             if(err.code == "UsernameExistsException"){ // err.codeの値を見て判断すればよい
               alert("登録済みだよ");
             }
             if(err.code == "InvalidParameterException"){
               alert("IDもしくはパスワードが間違っているよ");
             }
             return;
          }
          cognitoUser = result.user;
          alert('user name is ' + cognitoUser.getUsername());
        });
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?