Following the documented steps for using AWS Amplify DataStore with JavaScript does not work with Expo (using eas build
).
Following those instructions exactly, the simple 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, results in the error:
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
The instructions also seem incomplete, as they mention “Initializing the Amplify DataStore” but don’t really explain how that is supposed to happen (which might be the problem?).
How do I get past this error and get DataStore working in my app? What are the actual steps to get it working?
In case it matters, I’m building using
eas build --profile development --platform ios --local
and running using
expo start --dev-client
starting with a simple TypeScript expo init
project and having installed the required packages with
expo install aws-amplify aws-amplify-react-native
expo install @react-native-community/netinfo @react-native-async-storage/async-storage @react-native-picker/picker
and having confirmed that the app works fine with AWS Amplify GraphQL , e.g. with (instead of the above code)
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) )