简体   繁体   English

AWS 放大数据存储不会与服务器同步

[英]AWS amplify datastore wont synchronise with server

So I'm trying to get AWS Amplify's datastore library working with a reactJS web app I'm building.所以我试图让 AWS Amplify 的数据存储库与我正在构建的 reactJS Web 应用程序一起工作。 I was sending data to and from the api just fine using the graphql mutations manually, and then I tried to set up DataStore by following this tutorial .我手动使用 graphql 突变将数据发送到 api 和从 api 发送数据,然后我尝试按照本教程设置 DataStore。 Everything is working fine in the client with the indexed DB, but I can't get the data to synchronise with the api.带有索引数据库的客户端一切正常,但我无法让数据与 api 同步。 I get the following error in the console:我在控制台中收到以下错误:

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

I doubled checked, and I'm definitely including my AWS export as so:我仔细检查了一遍,我肯定将我的 AWS 导出包括在内:

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

Amplify.configure(awsExports);

And the content of the generated ./aws-exports.js is the following:生成的./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;

I've double checked the credentials for aws_appsync_graphqlEndpoint and aws_appsync_apiKey and they match the values returned from amplify status in the cli.我已经仔细检查了aws_appsync_graphqlEndpointaws_appsync_apiKey的凭据,它们与 cli 中amplify status返回的值相匹配。 I've also run amplify push and my Api has a status of No Change .我还运行了amplify push ,并且我的 Api 的状态为No Change When setting up the api I made sure to enable the conflict resolution too, as specified in the tutorial.在设置 api 时,我确保也启用了冲突解决,如教程中所述。

Interestingly, I also tried the following when setting up amplify:有趣的是,我在设置放大时也尝试了以下方法:

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

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

This solved the DataStore - Data won't be synchronised error, but I got the following errors instead, and the data still isn't being sent to the api.这解决了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

Does this mean my API is somehow not configured properly?这是否意味着我的 API 配置不正确? How Would I go about fixing it?我将如何解决它?

I have been fiddling with the DataStore syncing for a while in a Angular project.在 Angular 项目中,我一直在摆弄 DataStore 同步。 Here is what I think I learned, from my observations or answers posted by others这是我认为我从其他人发布的观察或答案中学到的东西

  1. Can't use API key authentication, that does not work.无法使用 API 密钥身份验证,这不起作用。 I use Cognito user pool and that works我使用Cognito用户池并且有效
  2. Had to enable Optimistic Concurrency as conflict resolution strategy, but I think it works even with the default Auto Merge , not sure exactly when is the first needed必须启用Optimistic Concurrency作为冲突解决策略,但我认为即使使用默认的Auto Merge也可以使用,不确定第一次需要的确切时间
  3. All types need an auto-generated id of type "ID!".所有类型都需要一个自动生成的“ID!”类型的 id。 Without that, you get errors when the listeners are setup and syncing does not work even for types that have it.否则,当设置侦听器时会出现错误,并且即使对于具有它的类型,同步也不起作用。
  4. Make sure the code is regenerated and pushed, and DataStore stays enabled after each schema change (sometimes that setting may reset)确保重新生成并推送代码,并且每次架构更改后 DataStore 保持启用状态(有时该设置可能会重置)

For troubleshooting, enable tracing ( Amplify.Logger.LOG_LEVEL = 'DEBUG' ) and filter in browser's console for messages that contain "err".对于故障排除,启用跟踪 ( Amplify.Logger.LOG_LEVEL = 'DEBUG' ) 并在浏览器的控制台中过滤包含“err”的消息。 If any such messages exist, the entire sync does not work.如果存在任何此类消息,则整个同步将不起作用。 That is how you can tell if you have one of the issues above.这就是您如何判断您是否有上述问题之一。

Another trick is to delete the local amplify-datastore IndexDB in Chrome Dev Tools' Application/Storage.另一个技巧是删除 Chrome Dev Tools 的 Application/Storage 中的本地amplify-datastore IndexDB If sync works, it will be re-populated from the DynamoDB table data in the cloud next time you reload your app.如果同步有效,下次您重新加载应用程序时,它将从云中的 DynamoDB 表数据中重新填充。

This must be a problem, coming up with a recent update.这一定是个问题,最近更新了。 About a week ago I successfully used DataStore (v2.2.8) in an expo app.大约一周前,我在一个 expo 应用程序中成功使用了 DataStore (v2.2.8)。 Yesterday, I started a new expo app project and cannot figure out a way to get the DataStore up and running with sync.昨天,我开始了一个新的 expo 应用程序项目,但无法找到让 DataStore 同步运行的方法。 Did you try an older version of @aws-amplify/datastore?您是否尝试过旧版本的 @aws-amplify/datastore?

I got the same error when I imported DataStore from aws-amplify instead of @aws-amplify/datastore .当我从aws-amplify而不是@aws-amplify/datastore导入DataStore时,我遇到了同样的错误。 It took me a while to figure it out but all I had to do was change the import statement.我花了一段时间才弄明白,但我所要做的就是更改导入语句。

So, try changing your import statement from:因此,尝试从以下位置更改您的导入语句:

import { DataStore } from "aws-amplify";

to:至:

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

Somewhere from between @aws-amplify/datastore version 2.9.6 to 2.9.9 they changed the Amplify Configure.在 @aws-amplify/datastore 版本 2.9.6 到 2.9.9 之间的某个地方,他们更改了 Amplify 配置。 You now need to put your aws-exports in {config: } However, I can not get 2.9.9 to load data, so I'm staying on 2.9.8您现在需要将您的aws-exports放入{config: }但是,我无法让 2.9.9 加载数据,所以我留在 2.9.8

2.9.8 and before: 2.9.8 及之前版本:

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

2.9.9: 2.9.9:

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

In addition to what other people have said.除了其他人所说的。 Try removing the node modules & package-lock.json .尝试删除node modulespackage-lock.json Then run npm cache clean --force and then npm i .然后运行npm cache clean --force然后npm i This worked for me with just normal Amplify.configure(config)这对我来说只适用于普通Amplify.configure(config)

In the root of your application, do this:在应用程序的根目录中,执行以下操作:

import { Amplify } from "aws-amplify";
import config from "../src/aws-exports.js";

Amplify.configure({
  ...config,
});

That should fix it for you.那应该为你解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM