简体   繁体   中英

How can I get AWS Amplify's DataStore to sync between user sessions in React?

Using aws-amplify@3.3.11 and @aws-amplify/datastore@2.9.0 with Cognito User Pools, I can do CRUD operations on model instances, and confirm they make it to the cloud. However, the events don't seem to sync back down from the cloud when I'm logged in to the same account from another browser, creating a different user session. The one potential wrinkle is that my model uses two @auth directives:

type TestModel
  @model
  @auth(rules: [
      { allow: owner, operations: [create, delete, update] },
      { allow: owner, ownerField: "members", operations: [read]}]) {
    id: ID!
    name: String!
    owner: String!
    members: [String]
}

I don't think permissions are causing the problem, since I can see the state I expect for the different users when I reload the page. It's just that the subscriptions never seem to sync down .

I'm new to React, and just trying to make sure DataStore sync works the way I would expect, so this code is pretty sloppy. Here's how I'm setting up the subscriptions:

    useEffect(() => {
        const dispatchDataStoreEvent = (msg) => {
            switch(msg.opType) {
                case 'INSERT':
                case 'UPDATE': {
                    console.log('TK dispatchDataStoreEvent UPSERT typeof ', typeof msg.element, msg.element)
                    dispatch(upsertOne(msg.element))
                    return
                }
                case 'DELETE': {
                    console.log('TK dispatchDataStoreEvent DELETE typeof ', typeof msg.element, msg.element)
                    dispatch(vaultDeleted(msg.element))
                    return
                }
                default: {
                    console.log('TK dataStoreDispatcher unhandled optype: ', msg.opType, msg.element)
                    return
                }
            }
        }
        const subscription = DataStore.observe(TestModel, c => c.members('contains', currentUser)).subscribe(msg => {
            dispatchDataStoreEvent(msg)
        })
        return () => subscription.unsubscribe()
    }, [dispatch, currentUser, testModels])

The predicate doesn't seem to matter, either – when I don't use it, the same expected behavior (sync events triggering from actions taken in a different user session) is missing. As far as I can tell, my auto-generated aws-exports.js file matches the this subscription spec . The CLI has a special section about authorizing subscriptions but I'm not sure if there's another argument I should be passing in for my call to DataStore.observe . Does DataStore only handle what it calls "[Observe] Data in real-time" in such a way that it never syncs changes down dynamically?

I can get the behavior I expect toggling my computer's network connection on and off. I just can't get DataStore to sync when changes are made.

Looks like it's an open issue with amplify-js, a limitation with using an array of owners as an auth rule: DataStore with @auth - subscriptionError... MissingFieldArgument: Missing field argument editors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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