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

Amplify中sendCustomChallengeAnswer方法不存在的报错求助

解决AWS Amplify sendCustomChallengeAnswer 不存在的类型报错

1. 升级AWS Amplify Auth版本

sendCustomChallengeAnswer 是Auth模块较新版本引入的方法,低版本会出现此报错。执行命令升级到最新稳定版:

npm update @aws-amplify/auth aws-amplify
# 或使用yarn
yarn upgrade @aws-amplify/auth aws-amplify

2. 修正Auth导入方式

默认导入的TypeScript类型定义可能存在兼容问题,改为命名导入:

import { Amplify } from "aws-amplify";
import { Auth } from '@aws-amplify/auth'; // 替换原默认导入
import awsconfig from '@/aws-exports';

Amplify.configure(awsconfig);

3. 确认Session参数类型

确保传入的session是正确的CognitoUserSession类型,若类型丢失可手动断言:

import { CognitoUserSession } from '@aws-amplify/auth';

verifyOtp: () => {
  Auth.sendCustomChallengeAnswer(session as CognitoUserSession, otp)
  .then((user) => {
    setUser(user);
    setMessage(SIGNEDIN);
  })
  .catch((err) => {
    setMessage(err.message);
    setOtp('');
    console.log(err);
  });
},

4. 清除TypeScript缓存并重启编辑器

TypeScript缓存可能导致类型识别异常,执行以下操作:

  • 删除项目下的node_modules/.cache目录(若存在)
  • 重启编辑器,执行npx tsc --noEmit重新检查类型

5. 验证aws-exports配置

确保aws-exports.js中Auth配置开启自定义认证流:

// aws-exports.js示例配置
export default {
  Auth: {
    region: '你的区域',
    userPoolId: '你的用户池ID',
    userPoolWebClientId: '你的客户端ID',
    authenticationFlowType: 'CUSTOM_AUTH' // 必须开启此配置才能使用自定义挑战
  }
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 20:29:57