“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中,显示无法订阅之类的错误......(参见上面的错误)。
有人知道如何解决这个问题吗?
谢谢
API.graphql()
的结果可以是两者,当您查询数据以立即返回时为
Promise
,或者为订阅时为
Observable
,您需要告诉 TypeScript 期望什么:
(API.graphql(
graphqlOperation(onCreateEmployee)
) as unknown as Observable<YourEventDataType>)
.subscribe(
(eventData) => { console.log(eventData) }
);
let graphqlCallPromise: Promise<any>;
if(graphqlCall instanceOf Observable) graphqlCallPromise = API.graphql(graphqlOperation(onCreateEmployee)).toPromise();
graphqlCallPromise.then((eventData) => { console.log(eventData) });
any
并绕过它
(API.graphql(
graphqlOperation(onCreateEmployee)
) as any).subscribe((eventData) => { console.log(eventData)
});