1

Con el servicio bookService.getBooksWithAuthorName() cargo en mi observable bookList$ todos los libros que tengo en mi db. Cada libro tiene una propiedad url_image y necesito limpiarla, para esto utilice un map (no se si es lo correcto) en el bookList$ en donde intento pasarle a la funcion linkImg() esta url_image. Pero Angular en la funcion getBooksWithAuthorName() me informa el siguiente error:

Type 'Observable<string[]>' is not assignable to type 'Observable<Book[]>'. Type 'string[]' is not assignable to type 'Book[]'. Type 'string' is not assignable to type 'Book'.ts(2322)

aca component.ts

bookList$: Observable<Book[]>;

ngOnInit(): void {
   this.getBooksWithAuthorName();
}

getBooksWithAuthorName() {
    this.bookList$ = this.bookService.getBooksWithAuthorName()
    .pipe(
      map((books: Book[]) => books.map(book => this.linkImg(book.url_image))     
    ));
  }

  linkImg(urlImage) {
    // quito la palabra public
    let str = urlImage.replace(/public/g, '');
    // quito la barra '\'
    str = str.replace('\\', '');
    // invierto la barra en sentido a '/'
    str = str.replace('\\', '/');
    // console.log(str);
    const URL = 'http://localhost:4000/';
    const link = URL + str;
    // console.log(link);
    return link;
  }

aca el servicio book.service.ts

getBooksWithAuthorName() {
   return this.http.get<Book[]>(this.URL_API + 'AuthorName');
}

1 respuesta 1

2

Se debe a que lo siguiente esta retornando un string que es el book.url.image:

 map((books: Book[]) => books.map(book => this.linkImg(book.url_image))   

Necesitas retornar el objeto, lo correcto sería:

 map((books: Book[]) => books.map(book => { 
   return {
     ...book , 
     url_image: this.linkImg(book.url_image)
   } 
 })   

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.