“Promise”类型上不存在属性“subscribe”| |可观察<GraphQLResult>'<object>

问题描述 投票:0回答:3
在 Ionic 应用程序中,我们正在集成 AWS-Amplify,我们可以插入和获取数据。但是在实现订阅时,我们收到编译器错误说

“Promise | 类型”上不存在属性“subscribe”可观察到’。 “Promise”类型上不存在属性“subscribe”。

在 Subscription.ts 中

export const onCreateEmployee = `subscription OnCreateEmployee( $employee_id: Int $employee_name: String ) { onCreateEmployee( employee_id: $employee_id employee_name: $employee_name ) { employee_id employee_name } }

在 Main.ts 中

const subscription = API.graphql( graphqlOperation(onCreateEmployee) ).subscribe((eventData) => { console.log(eventData) });

在main.ts中,显示无法订阅之类的错误......(参见上面的错误)。

有人知道如何解决这个问题吗?

谢谢

angular amazon-web-services ionic2 amazon-dynamodb aws-amplify
3个回答
1
投票
由于

API.graphql()

 的结果可以是两者,当您查询数据以立即返回时为 
Promise
,或者为订阅时为 
Observable
,您需要告诉 TypeScript 期望什么:

(API.graphql( graphqlOperation(onCreateEmployee) ) as unknown as Observable<YourEventDataType>) .subscribe( (eventData) => { console.log(eventData) } );
    

0
投票
这是一种解决方法,但你可以将 Observable 转换为 Promise

let graphqlCallPromise: Promise<any>; if(graphqlCall instanceOf Observable) graphqlCallPromise = API.graphql(graphqlOperation(onCreateEmployee)).toPromise(); graphqlCallPromise.then((eventData) => { console.log(eventData) });
    

-1
投票
你将它投射到

any

并绕过它

(API.graphql( graphqlOperation(onCreateEmployee) ) as any).subscribe((eventData) => { console.log(eventData) });
    
© www.soinside.com 2019 - 2024. All rights reserved.