簡體   English   中英

Angular中的httpclient請求數組的問題

[英]issue with httpclient request Array in Angular

我正在使用httpclient請求從wordpress API獲取數據,作為響應,我遇到了錯誤。 似乎Array有問題。 這是我正在使用的代碼

getTetimonialData() {
    this.http.get<{ testimonials: any }>(BASE_URL + '/testimonial/').pipe( map( testimonialsData => {
      // return testimonialsData;
      return {
        testimonials: testimonialsData.testimonials.map(testData => {
          return {
            id: testData.id,
            date: testData.date,
            slug: testData.slug,
            title: {
              rendered: testData.title.rendered,
            },
            yoast_meta: {
              yoast_wpseo_title: testData.yoast_meta.yoast_wpseo_title,
              yoast_wpseo_metadesc: testData.yoast_meta.yoast_wpseo_metadesc,
              yoast_wpseo_canonical: testData.yoast_meta.yoast_wpseo_canonical,
            },
            acf: {
              video_url: testData.acf.video_url,
              video_image: {
                url: testData.acf.video_image.url,
                alt: testData.acf.video_image.alt,
              }
            }
          };
        })
      };
    })).subscribe(transformData => {
      this.testimonials = transformData.testimonials;
      console.log(this.testimonials);
      this.testimonialFetch.next(this.testimonials);
    });
  }

這是錯誤

ERROR TypeError: Cannot read property 'map' of undefined

我什至嘗試使用tesimonialData.map因為響應已經在Array中了,但這也顯示了tesimonialData.map ,不允許任何推薦類型。

此錯誤適用於我不使用rxjs map函數遍歷數據的行。

所以我做錯了嗎?

您的GET請求類型不正確。 以下是您在評論中分享的URL的圖片。

API響應

代替this.http.get<{ testimonials: any }>...它應該是this.http.get<any[]>...因為響應是推薦書的列表,而不是帶有testimonials財產。 理想情況下,您將有一個用於類型安全的接口,因此代替any[]可能是ServerTestimonial[]

您能否回答這個問題以及Angular Resolver問題

對不起,沒有太多聲譽可言

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM