在 React Native 和 Expo 上使用 aws amplify datastore 时出现错误 "Node is not supported"

Getting error "Node is not supported" using aws amplify datastore on react native and expo

AWS 在 re:invent 2019 年推出了 Amplify DataStore。Datastore 是一个持久的设备存储库,供开发人员写入、读取和观察数据变化。

根据公告,Amplify DataStore 是否允许开发人员编写利用分布式数据的应用程序,而无需为离线或在线场景编写额外的代码。 Amplify DataStore 可用作 Web 和移动应用程序中的独立本地数据存储,无需连接到云,也无需拥有 AWS 账户。引擎盖下是连接到应用程序同步 graphQl API 的数据存储。很棒的功能是易于使用和离线功能。另见公告 https://aws.amazon.com/de/blogs/aws/amplify-datastore-simplify-development-of-offline-apps-with-graphql/ or doc https://aws-amplify.github.io/docs/js/datastore

不过,amplify 支持流行的 Web 框架,例如 Angular、React 和 Vue。它还支持使用 React Native 开发的移动应用程序,Swift 用于 iOS,或 Java 用于 Android。

宣布后,我有动力使用基于 React Native 应用程序和 expo 的功能。

但是在 React Native 和 expo 上使用 aws amplify datastore 时出现错误 "Node is not supported"。

[Unhandled promise rejection: Error: Node is not supported]
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:63717:20 in getDefaultAdapter
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:63730:47 in Storage
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:64010:31 in ExclusiveStorage
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:61426:81 in <unknown>
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60761:23 in step
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60663:9 in <unknown>
- node_modules/promise/setimmediate/core.js:45:7 in tryCallTwo
- node_modules/promise/setimmediate/core.js:200:23 in doResolve
- node_modules/promise/setimmediate/core.js:66:12 in Promise
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60642:34 in <unknown>
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:61204:17 in <unknown>
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60761:23 in step
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60663:9 in <unknown>
- node_modules/promise/setimmediate/core.js:45:7 in tryCallTwo
- node_modules/promise/setimmediate/core.js:200:23 in doResolve
- node_modules/promise/setimmediate/core.js:66:12 in Promise
- node_modules/@aws-amplify/datastore/dist/aws-amplify-datastore.js:60642:34 in <unknown>
* App.js:43:40 in loadData$

导入的数据存储和模型:

import { DataStore } from "@aws-amplify/datastore";
import { Post } from "./src/models";
import amplify from './aws-exports';
Amplify.configure(amplify);

并使用数据存储创建加载数据 API:

  async loadData() {
    await DataStore.save(
      new Post({
        name: `My First Post`
      })
    ).catch(err => {
      console.error(err)
    });

    const posts = await DataStore.query(Post);
    console.log(posts)
  }

依赖关系:

    "dependencies": {
        "@aws-amplify/api": "^2.1.1",
        "@aws-amplify/core": "^2.2.0",
        "@aws-amplify/datastore": "^1.0.2",
        "@aws-amplify/pubsub": "^2.1.1",
        "aws-amplify": "1.2.2",
        "expo": "^35.0.0",
        "react": "16.8.3",
        "react-dom": "16.8.3",
        "react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
        "react-native-web": "^0.11.7"
    }

目前库还没有为 React Native 准备好 参考:https://github.com/aws-amplify/amplify-js/issues/4527

React Native 现在支持 DataStore。您可以在此处找到安装说明:https://aws-amplify.github.io/docs/js/datastore#for-react-native