AWSAmplifyRESTAPI找不到API
我正在使用 AWS Amplify,因为我必须在没有框架(没有 React,没有 Angular:只有普通 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 资源
回答
我想补充一点,@Deiv 建议 API.configure() 是正确的,这对我有用。尝试后amplify pull,amplify 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