AWS放大数据存储文档之后的错误

发布于 2025-01-29 07:12:14 字数 2973 浏览 4 评论 0 原文

When I follow the AWS

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

import { DataStore } from 'aws-amplify';

try {
  const data = await DataStore.query(TestData);
  console.log("Data retrieved successfully!", JSON.stringify(data, null, 2));
} catch (error) {
  console.log("Error retrieving data", error);
}

,其中 testData 在我的 schema.graphql 中定义,如文档所述,给出了

Unexpected identifier '_awsAmplify'. Expected ';' after variable declaration.
at node_modules/metro-runtime/src/modules/HMRClient.js:106:4 in inject
at node_modules/metro-runtime/src/modules/HMRClient.js:115:2 in injectUpdate
at node_modules/metro-runtime/src/modules/HMRClient.js:183:20 in on$argument_1
at node_modules/metro-runtime/src/modules/vendor/eventemitter3.js:229:10 in emit
at node_modules/metro-runtime/src/modules/HMRClient.js:162:10 in _ws.onmessage
at node_modules/react-native/Libraries/WebSocket/WebSocket.js:229:8 in _eventEmitter.addListener$argument_1
at node_modules/react-native/Libraries/vendor/emitter/_EventEmitter.js:135:10 in EventEmitter#emit

此错误是什么意思,我如何避免?

有一个隐秘的语句在文档中

@aws-amplify/datastore 依赖项已添加到项目中时,插件开始使用时自动初始化。

但是,没有说明添加该依赖关系(在任何情况下,这是 aws-amplify 的一部分,作为 datastore 证明的成功导入)。

明确添加 (忽略警告已经存在的警告)和 amazon-cognito-nistity-js 作为依赖关系无法解决问题。


相同的项目可以与GraphQl一起使用,例如,如果我将上述内容替换

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

const listTestData = `
  query ListTestData(
    $filter: ModelTestDataFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listTestData(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        name
        categories
        createdAt
        updatedAt
      }
    }
  }
`
import { API, graphqlOperation } from 'aws-amplify'
API.graphql(graphqlOperation(listTestData, { filter: {},  limit: 50 })).then( f => console.log(f) )

eas build --profile development --platform ios --local

expo start --dev-client

When I follow the AWS Amplify documentation for setting up DataStore using the CLI,
the following code

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

import { DataStore } from 'aws-amplify';

try {
  const data = await DataStore.query(TestData);
  console.log("Data retrieved successfully!", JSON.stringify(data, null, 2));
} catch (error) {
  console.log("Error retrieving data", error);
}

where TestData is defined in my schema.graphql as described on the docs, gives

Unexpected identifier '_awsAmplify'. Expected ';' after variable declaration.
at node_modules/metro-runtime/src/modules/HMRClient.js:106:4 in inject
at node_modules/metro-runtime/src/modules/HMRClient.js:115:2 in injectUpdate
at node_modules/metro-runtime/src/modules/HMRClient.js:183:20 in on$argument_1
at node_modules/metro-runtime/src/modules/vendor/eventemitter3.js:229:10 in emit
at node_modules/metro-runtime/src/modules/HMRClient.js:162:10 in _ws.onmessage
at node_modules/react-native/Libraries/WebSocket/WebSocket.js:229:8 in _eventEmitter.addListener$argument_1
at node_modules/react-native/Libraries/vendor/emitter/_EventEmitter.js:135:10 in EventEmitter#emit

What does this error mean and how do I avoid it?

There is a cryptic and possibly relevant (and not quite grammatically correct) statement in the documentation that

When the @aws-amplify/datastore dependency is added to the project, the plugin is automatic initialized when you start using.

But no instructions to add that dependency (which is in any case case clearly available as part of aws-amplify as the successful import of DataStore demonstrates).

Explicitly adding both @aws-amplify/datastore (ignoring warnings that it is already there) and amazon-cognito-identity-js as dependences does not fix the issue.


The same project works fine with GraphQL, e.g., if I replace the above with something like:

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

const listTestData = `
  query ListTestData(
    $filter: ModelTestDataFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listTestData(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        name
        categories
        createdAt
        updatedAt
      }
    }
  }
`
import { API, graphqlOperation } from 'aws-amplify'
API.graphql(graphqlOperation(listTestData, { filter: {},  limit: 50 })).then( f => console.log(f) )

In case it matters, I'm building using

eas build --profile development --platform ios --local

and running using

expo start --dev-client

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文