放大未捕获的 AppSync 订阅(承诺中)

Posted

技术标签:

【中文标题】放大未捕获的 AppSync 订阅(承诺中)【英文标题】:Amplify AppSync Subscription Uncaught (in promise) 【发布时间】: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",

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于放大未捕获的 AppSync 订阅(承诺中)的主要内容,如果未能解决你的问题,请参考以下文章

未捕获(承诺)

错误:`未捕获(承诺中)类型错误:无法读取未定义的属性'doc'`

GraphQL + Apollo 错误:未捕获(承诺中)

vue-resource:在拦截 ajax 错误时捕获“未捕获(在承诺中)”

405 - 未捕获(承诺中)错误:请求失败,状态码为 405

Vue:未捕获(承诺中)TypeError:无法读取未定义的属性'_c'