I am calling an API from my component.ts file using angular. API call is successful, however I am not able to read the data from my response.
If I print from event
, I can get the data however if I print from the this.book there is no data.
Error I am getting is error TS2339: Property 'name' does not exist on type '{}'.
component.ts
ngOnInit(): void {
this.activatedRoute.paramMap.subscribe((params: ParamMap) => {
this.id = params.get('id');
this.api.GetBook(this.id).then((event) => {
this.book = event;
console.log(event.name)// I can get response
console.log(this.book.name) //Error
});
})
Data in event or this.book is:
{__typename: 'Book', id:'1', name: "test"}
CodePudding user response:
You can try the following:
console.log(this.book['name']);
CodePudding user response:
You can assign a type to the event parameter, which will get rid of the type error
this.api.GetBook(this.id).then((event: { id: string; name: string }) => {
...
}
or a better way is to assign a return type for the GetBook method
GetBook(id):Promise< { id: string; name: string }> {
return new Promise((resolve, reject) => {...});
}