In Ionic application, we are integrating AWS-Amplify and we can able to insert and fetch the data. But while implementing subscription we are getting compiler error says
Property 'subscribe' does not exist on type 'Promise | Observable'. Property 'subscribe' does not exist on type 'Promise'.
In 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
}
}
In Main.ts
const subscription = API.graphql(
graphqlOperation(onCreateEmployee)
).subscribe((eventData) => { console.log(eventData)
});
In main.ts, it shows the error like cannot subscribe ..... (see the above error).
Can anybody know how to resolve this?
Thanks
As the result from API.graphql() can be both, a Promise when you query data for immediate return and alternatively an Observable for subscriptions, you need to tell TypeScript what to expect:
(API.graphql(
graphqlOperation(onCreateEmployee)
) as unknown as Observable<YourEventDataType>)
.subscribe(
(eventData) => { console.log(eventData) }
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With