5

所以我试图让 AWS Amplify 的数据存储库与我正在构建的 reactJS Web 应用程序一起工作。我手动使用 graphql 突变将数据发送到 api 和从 api 发送数据,然后我尝试按照本教程设置 DataStore 。带有索引数据库的客户端一切正常,但我无法让数据与 api 同步。我在控制台中收到以下错误:

 DataStore - Data won't be synchronized. No GraphQL endpoint configured. Did you forget `Amplify.configure(awsconfig)`?

我仔细检查了一遍,我肯定将我的 AWS 导出包括在内:

import Amplify from "@aws-amplify/core";
import awsExports from "./aws-exports";

Amplify.configure(awsExports);

生成的内容./aws-exports.js如下:

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "us-east-2",
    "aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxxxxx.appsync-api.us-east-2.amazonaws.com/graphql",
    "aws_appsync_region": "us-east-2",
    "aws_appsync_authenticationType": "API_KEY",
    "aws_appsync_apiKey": "xxxxxxxxxxxxxx",
    "aws_cognito_identity_pool_id": "us-east-2:xxxxxxxxxxxxx",
    "aws_cognito_region": "us-east-2",
    "aws_user_pools_id": "us-east-2_xxxxxxxxx",
    "aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxx",
    "oauth": {}
};


export default awsmobile;

我已经仔细检查了凭据,aws_appsync_graphqlEndpoint并且aws_appsync_apiKey它们与amplify statuscli 中返回的值相匹配。我也跑amplify push了,我的 Api 的状态为No Change. 在设置 api 时,我确保也启用了冲突解决,如教程中所述。

有趣的是,我在设置放大时也尝试了以下方法:

import Amplify from "@aws-amplify/core";
import {DataStore} from "@aws-amplify/datastore";
import awsExports from "./aws-exports";

Amplify.configure(awsExports);
DataStore.configure(awsExports);

这解决了DataStore - Data won't be synchronised错误,但我得到了以下错误,并且数据仍然没有被发送到 api。

DataStore - subscriptionError Subscribe only available for AWS AppSync endpoint
DataStore - Sync error Subscribe only available for AWS AppSync endpoint

这是否意味着我的 API 配置不正确?我将如何解决它?

4

5 回答 5

1

当我DataStore从而aws-amplify不是@aws-amplify/datastore. 我花了一段时间才弄明白,但我所要做的就是更改导入语句。

因此,尝试从以下位置更改您的导入语句:

import { DataStore } from "aws-amplify";

至:

import { DataStore } from "@aws-amplify/datastore";

于 2020-11-20T10:49:47.850 回答
1

在 Angular 项目中,我一直在摆弄 DataStore 同步。这是我认为我从其他人发布的观察或答案中学到的东西

  1. 无法使用 API 密钥身份验证,这不起作用。我使用Cognito用户池并且有效
  2. 必须启用Optimistic Concurrency冲突解决策略,但我认为它甚至可以使用 default Auto Merge,不确定第一次需要的确切时间
  3. 所有类型都需要一个自动生成的“ID!”类型的 id。否则,当设置侦听器时会出现错误,并且即使对于具有它的类型,同步也不起作用。
  4. 确保重新生成并推送代码,并且每次架构更改后 DataStore 保持启用状态(有时该设置可能会重置)

要进行故障排除,请启用跟踪 ( Amplify.Logger.LOG_LEVEL = 'DEBUG') 并在浏览器的控制台中过滤包含“err”的消息。如果存在任何此类消息,则整个同步将不起作用。这就是您如何判断您是否有上述问题之一。

amplify-datastore IndexDB另一个技巧是在 Chrome 开发工具的应用程序/存储中删除本地。如果同步有效,下次您重新加载应用程序时,它将从云中的 DynamoDB 表数据中重新填充。

于 2020-12-24T17:43:08.347 回答
1

这一定是个问题,最近更新了。大约一周前,我在一个 expo 应用程序中成功使用了 DataStore (v2.2.8)。昨天,我开始了一个新的 expo 应用程序项目,但无法找到让 DataStore 同步运行的方法。您是否尝试过旧版本的 @aws-amplify/datastore?

于 2020-09-02T07:00:39.347 回答
0

In addition to what other people have said. Try removing the node modules & package-lock.json. Then run npm cache clean --force and then npm i. This worked for me with just normal Amplify.configure(config)

于 2021-03-21T17:34:11.447 回答
0

在 @aws-amplify/datastore 版本 2.9.6 到 2.9.9 之间的某个地方,他们更改了 Amplify 配置。你现在需要把你aws-exports{config: } 但是,我不能让 2.9.9 加载数据,所以我留在 2.9.8

2.9.8 及之前版本:

import awsConfig from "./aws-exports";
import { Amplify} from "@aws-amplify/core";
Amplify.configure(awsConfig);

2.9.9:

import awsConfig from "./aws-exports";
import { Amplify} from "@aws-amplify/core";
Amplify.configure({ config: { awsConfig } });
于 2021-02-24T12:56:54.923 回答