0
booksList: Book[];
listById(id: number): Book{
this.http.get<Book>(`${url}/list/id=${id}`)
.subscribe((response)=> {
this.book = response;
console.log(this.book); //aqui existe o livro
return this.book; //acho que não está servindo para nada
});
console.log(this.book); //aqui não existe
return this.book; //acho que não está servindo para nada
}
want to persist the return of this information in this.book to use in other functions, for example:
selectedBook() {
let myBook = listById(1); (deve me retornar o objeto livro de id 1);
this.booksList.push(this.book);
console.log(booksList); //tem que existir o livro 1
}
However I can’t, as I commented in the code the variable always appears as Undefined, how to make the subscribe content persist outside of it?
I appreciate any help.
You have to return the observable and subscribe in your other way. You cannot return the direct book because the http call is asynchronous.
– Eduardo Vargas