【问题标题】:Google books api returns ERROR TypeError: Cannot read property 'thumbnail' of undefined Angular 8Google 图书 api 返回错误类型错误:无法读取未定义 Angular 8 的属性“缩略图”
【发布时间】:2020-02-05 04:36:01
【问题描述】:

我正在制作简单的 Angular Web 应用程序,用户可以在其中搜索书籍并返回不同的书籍。

它大部分工作正常,但对于某些搜索的标题,例如“neena”,它返回 TypeError: Cannot read property 'thumbnail' of undefined

我查看了控制台上的 http 响应,发现缺少一些书籍 books.volumeInfo.imageLinks 、 item.volumeInfo.imageLinks.thumbnail

我在这里Google books api returns missing parameters 看到了这个解决方案,但不知道如何在我的应用程序中使用它。

service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs';
import 'rxjs/Rx';


@Injectable({
  providedIn: 'root'
})
export class GoogleapiService {

  API_KEY = 'AIzaSyDHDCI5JYsbDRGRa5nx252a0kv43XwCpvE';

  constructor(private httpClient: HttpClient) { }

  getNews(searchText):Observable<any>{
    const encodedURI = encodeURI("https://www.googleapis.com/books/v1/volumes?q="+searchText+"&maxResults=12&key=");
    return this.httpClient.get(encodedURI);
  } 
}

组件.ts

import { Component, OnInit } from '@angular/core';
import {GoogleapiService} from '../services/googleapi.service';
import { NgForm } from '@angular/forms';
import {Books} from '../booksearch/books';


@Component({
  selector: 'app-booksearch',
  templateUrl: './booksearch.component.html',
  styleUrls: ['./booksearch.component.css']
})
export class BooksearchComponent implements OnInit {
  items;
  searchText;
  constructor(private googleapiservice: GoogleapiService) { }
  ngOnInit() { 
  }

  onSubmit(form:NgForm){
   this.searchText = form.value.searchVariable;
    this.googleapiservice.getNews(form.value.searchVariable).subscribe((data)=>{
      this.items = data['items'];
      console.log(data);

      //console.log("test");
      //console.log(this.items[1].volumeInfo.imageLinks.thumbnail);
      //console.log(form.value.searchVariable);
    });

  }  

}

component.html

<h1>BOOKS</h1>

<div class="container">
    <h1>search your books</h1>
    <form #searchForm="ngForm" (ngSubmit)="onSubmit(searchForm)">
        <div class="form-group">
            <input type="text" class="form-control mt-10" name="searchVariable" placeholder="search for books" autocomplete="off" ngModel/>
        </div>
            <button type="submit" class="btn btn-danger"> Search</button>
    </form>
</div>   
<div *ngFor="let item of items">
    <h2>{{item?.volumeInfo.title}}</h2>
    <p>{{item?.volumeInfo.authors}}</p>
    <p>{{item?.volumeInfo.description}}</p>
    <p>{{item.volumeInfo.imageLinks?.thumbnail}}</p>
    <img src={{item.volumeInfo.imageLinks.thumbnail}} alt="Smiley face" height="42" width="42">

</div>

我是 Angular 8 的新手,我不知道如何以及在哪里使用 constpaidBooks = apiResponse.data.filter(book => book.listPrice) 根据解决方案

请有人帮助我

谢谢

【问题讨论】:

    标签: angular api get


    【解决方案1】:

    问题出在您拥有 {{item.volumeInfo.imageLinks.thumbnail}} 的 component.html 行中 imageLinks 未定义,您无法获取未定义的缩略图。在获取缩略图之前,您必须检查 item.volumeInfo.imageLinks != undefined 是否。

    【讨论】:

    • books.google.com/books/…"}} alt="Smiley face">
      我怀疑控制台仍然会在 api 响应中显示缺失的参数
    • 它会显示,但您不会收到错误 TypeError: Cannot read property 'thumbnail' of undefined. You get the error because "item.volumeInfo.imageLinks" is undefined.你必须检查它是否未定义。检查您的 img 标签,这就是发生错误的地方。将 src={{item.volumeInfo.imageLinks.thumbnail}} 替换为 src={{item.volumeInfo.imageLinks?.thumbnail}}
    • 效果很好,如果我想用图像替换丢失的缩略图,有什么办法.. 非常感谢您的帮助
    • 这应该可以。你可以使用 *ngIf。如果 imageLinks 存在: 。如果 imageLinks 未定义或为空,这将显示您的替换图像: don不要忘记把你的图片 src 放到最后一个 img 标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2021-12-16
    • 2018-02-17
    • 2021-11-05
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多