React集成AWS后端报错:Uncaught (in promise) API api3ceaf69c不存在
问题:调用API时提示“Uncaught (in promise) API api3ceaf69c does not exist”
我在将现有后端集成到新React前端时,每次调用API都会抛出错误:Uncaught (in promise) API api3ceaf69c does not exist。此前代码能正常运行,直到我修改并重建了后端。
客户端代码
function getData() { const apiName = "api3ceaf69c"; const path = "/users"; const myInit = { headers: {}, }; return API.get(apiName, path, myInit); } (async function () { const response = await getData(); console.log(JSON.stringify(response)); })();
index.js
import { Amplify, API } from "aws-amplify"; import awsExports from "./aws-exports"; Amplify.configure(awsExports); API.configure(awsExports);
aws-exports.json
{ "aws_cloud_logic_custom": [ { "name": "api3ceaf69c", "endpoint": "https://xxxxxxx.execute-api.ap-south-1.amazonaws.com/dev", "region": "ap-south-1" } ] }
使用的aws-amplify版本为^4.3.27。
解决方案尝试
- 移除重复配置:删除
API.configure(awsExports),Amplify.configure(awsExports)已经完成所有模块的初始化,重复调用可能导致配置覆盖或冲突。 - 校验配置一致性:确认
aws-exports.json中aws_cloud_logic_custom下的name字段和代码中的apiName完全一致,包括大小写、特殊字符,没有拼写错误。 - 清除项目缓存:旧的后端配置可能被缓存,执行以下命令清理后重新构建:
rm -rf node_modules/.cache npm run build - 验证后端可用性:直接在浏览器或接口测试工具中访问
https://xxxxxxx.execute-api.ap-south-1.amazonaws.com/dev/users,确认后端API能正常返回响应,排除后端本身的问题。 - 锁定依赖版本:将aws-amplify版本固定为
4.3.27避免自动升级带来的兼容问题:npm install aws-amplify@4.3.27 --save-exact - 手动指定API端点:尝试手动配置API端点,替代自动导入的方式,测试是否能识别API:
import { Amplify, API } from "aws-amplify"; import awsExports from "./aws-exports"; Amplify.configure({ ...awsExports, API: { endpoints: awsExports.aws_cloud_logic_custom } });
内容的提问来源于stack exchange,提问作者Anurag
相关产品推荐
相关产品推荐

