You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用amazon-cognito-identity-js重发确认邮件时报错:Cannot set property 'username' of undefined

解决amazon-cognito-identity-js重发确认邮件时的TypeError错误

你遇到的Uncaught TypeError: Cannot set properties of undefined (setting 'username')错误,核心问题有两个:

  • 创建CognitoUser实例时未使用new关键字,导致生成的不是合法的CognitoUser对象,后续方法调用时内部无法正确设置属性
  • resendConfirmationCode方法的参数传递不符合官方规范,该方法不需要手动传入username和额外布尔参数

修正后的重发确认邮件方法

const resendConfirmation = () => {
    console.log(username);
    const userData = {
      Username: username,
      Pool: UserPool,
    };
    // 必须使用new关键字创建CognitoUser实例
    const cognitoUser = new CognitoUser(userData);
    // 正确调用resendConfirmationCode,仅传入回调函数即可
    cognitoUser.resendConfirmationCode((err, data) => {
      if (err) {
        console.log(err);
        alert("Couldn't resend code");
      } else {
        console.log(data);
        alert('code resent');
      }
    });
};

关键说明

  1. 必须使用new关键字:你的账号验证方法verifyAccount里正确使用了new CognitoUser(),而重发方法里漏掉了这一步,导致生成的不是具备完整属性和方法的CognitoUser实例。
  2. 简化方法参数:CognitoUser实例已经包含了用户名和用户池信息,resendConfirmationCode不需要重复传入username,只需要传入回调函数(或使用Promise方式调用)即可。

内容的提问来源于stack exchange,提问作者modisulak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 12:40:54