【问题标题】:Amplify AppSync Subscription Uncaught (in promise)放大未捕获的 AppSync 订阅(承诺中)
【发布时间】:2021-03-03 18:35:19
【问题描述】:

我正在尝试使用 AWS 放大 GraphQL 订阅,如下所示,

import Amplify,{ API, Storage, graphqlOperation } from "aws-amplify";
import awsmobile from "../../aws-exports";
Amplify.configure(awsmobile);
...
     const notiSubscription = API.graphql(graphqlOperation(onCreateNotification)).subscribe({
      next: (todoData) => {
        console.log(todoData);
      },
    });
...

onCreateNotification Graphql 是,

 subscription OnCreateNotification {
onCreateNotification {
  id
}}

以下是我得到的错误,

AWSAppSyncProvider.ts:204 Uncaught (in promise) undefined

rejected    @   AWSAppSyncProvider.ts:204
Promise.then (async)        
step    @   AWSAppSyncProvider.ts:204
(anonymous) @   AWSAppSyncProvider.ts:204
push../node_modules/@aws-amplify/pubsub/lib-esm/Providers/AWSAppSyncRealTimeProvider.js.__awaiter   @   AWSAppSyncProvider.ts:204
AWSAppSyncRealTimeProvider._startSubscriptionWithAWSAppSyncRealTime @   AWSAppSyncRealTimeProvider.ts:227
(anonymous) @   AWSAppSyncRealTimeProvider.ts:185
Subscription    @   Observable.js:197
subscribe   @   Observable.js:279
(anonymous) @   PubSub.ts:171
...

请帮帮我,我的配置也是

"aws_project_region": "us-east-1",
    "aws_appsync_graphqlEndpoint": "https://xxx.appsync-api.us-east-1.amazonaws.com/graphql",
    "aws_appsync_region": "us-east-1",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_appsync_apiKey": "xxxx",

【问题讨论】:

    标签: javascript reactjs graphql aws-amplify aws-appsync


    【解决方案1】:

    您需要在 useEffect() 挂钩中初始化订阅,然后取消订阅。

     useEffect(() => {
        const subscription = API.graphql(graphqlOperation(onCreateNotification))
          .subscribe({
            next: todoData => {
            console.log(todoData);
            }
          })
          return () => subscription.unsubscribe()
      }, [])
    

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 2017-11-24
      • 2017-09-21
      • 2021-07-14
      • 2021-11-15
      • 2019-11-03
      • 2019-04-18
      • 2017-07-29
      • 1970-01-01
      相关资源
      最近更新 更多