【问题标题】:AWS Amplify REST API cannot find the APIAWS Amplify REST API 找不到 API
【发布时间】:2021-03-19 14:30:22
【问题描述】:

我正在使用 AWS Amplify,因为我必须在没有框架的 legacy 应用程序中引入它的一些功能(没有 React,没有 Angular:只有 vanilla JS)。

我成功使用了 Auth 模块,因此我重新创建了一个简单的注册/登录/退出策略。我现在想要的是使用 REST API 模块调用 REST API。因此,我使用 API Gateway PetStore 示例创建了一个 API,并与之交互。

所以我创建了配置:

import Amplify from '@aws-amplify/core';

Amplify.configure({
    Auth: {
       ...
    },
    API: {
        endpoints: [
            {
                name: "PetStore", // name of the API in API Gateway console
                endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
                region: "eu-central-1",
                paths: ['/']
            }
        ]
    }
});

还有其他地方:

import Api from '@aws-amplify/api-rest'; 

Api.get('PetStore', '/pets', {})
   .then(response => {
       console.log(response)
   }).catch(error => {
       console.log(error);
   });

但是,当执行时,我总是得到同样的错误:

API PetStore 不存在

有什么想法吗?请记住:

  • API 已经存在
  • 我不想使用 Amplify CLI 创建 AWS 资源

【问题讨论】:

    标签: amazon-web-services aws-amplify


    【解决方案1】:

    我想补充一点,@Deiv 建议 API.configure() 是正确的,这对我有用。尝试amplify pullamplify push 后,我仍然收到错误API <api-name> does not exist

    所以我的代码现在看起来像这样:

    import Amplify, { API } from 'aws-amplify';
    import awsconfig from './aws-exports';
    
    Amplify.configure(awsconfig);
    API.configure(awsconfig);
    

    我正在使用 npm 包:aws-amplify 3.3.21

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我设法通过直接在“Api”对象上配置导出来解决它:

      API.configure(aws_exports);
      

      以前只需在 Amplify.configure 上设置它就对我有用,但似乎较新版本的 Amplify 需要在子模块本身上使用它。

      在这个长期存在的问题中可以找到更多的点点滴滴(其中为 Auth 弹出了相同的问题,但在我的情况下我对这两个问题都应用了相同的问题并修复了它):https://github.com/aws-amplify/amplify-js/issues/4315

      【讨论】:

        【解决方案3】:

        你需要在调用Amplify.configure之后调用API.configure(),否则你的API设置将不会被应用,因此它返回API PetStore does not exist

        import Amplify, { API } from 'aws-amplify'; // this part depends on which Amplify you are using. but make sure to import API
        
        
        Amplify.configure({
            Auth: {
               ...
            },
            API: {
                endpoints: [
                    {
                        name: "PetStore", // name of the API in API Gateway console
                        endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
                        region: "eu-central-1",
                        paths: ['/']
                    }
                ]
            }
        });
        
        // Call this 
        API.configure(); // no awsconfig required as you have set your own
        

        【讨论】:

          【解决方案4】:

          API * does not exist 通常意味着您尚未推送您创建的 REST API。如果不能使用amplify push是不是通过控制台手动创建和放大API?

          【讨论】:

            【解决方案5】:

            我们在 package.json 中将 aws-amplify 的版本从 3 改回了以前的版本 "aws-amplify": "^2.2.2" 并解决了这个问题。

            Think 版本 3 打破了 aws-amplify 的手动配置。

            尝试将版本更改为您知道可以使用的先前版本。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-06-30
              • 2023-03-27
              • 2022-08-15
              • 1970-01-01
              • 1970-01-01
              • 2020-03-08
              • 1970-01-01
              • 2021-08-31
              相关资源
              最近更新 更多