정의되지 않은 '필터'속성을 찾을 수 없습니다.

모세

온라인 상점 프로젝트 (Angular)를 설정하고 각 제품 페이지에서 일종의 "추천"영역을 수행하려고했습니다.

처음에는 모든 제품을 구독 한 다음 일치하는 유형과 ID를 찾기 위해 살펴보고 싶었습니다.

테스트를 위해 MongoDB에서 18 개의 제품 (6 개의 다른 제품)을 만들었지 만 (같은 유형 및 ID를 가진 동일한 제품의 3 개 제품) 불행히도이 오류가 발생하여 이유를 알 수 없습니다.

ProductComponent.findRecommandedProducts에서 정의되지 않은 '필터'속성을 읽을 수 없습니다.

export class ProductComponent implements OnInit {

  recommandedProducts: Product[];
  allProducts: Product[];
  product: Product;


  constructor(private productService: ProductService, private route: Router, private actRoute: ActivatedRoute) { }

  ngOnInit() {
    this.recommandedProducts = this.findRecommandedProducts(this.product)
  };

  findRecommandedProducts(currectProduct: Product){
    this.productService.getProducts().subscribe((data: Product[]) => {
      this.allProducts = data;
    });
//productService.getProducts() returns all of my products in JSON format to localhost//

    let recommandedProducts = this.allProducts.filter(otherProduct => 
      otherProduct.type == currectProduct.type && otherProduct.id == currectProduct.id) 

      // otherProduct.id == currectProduct.id becacuse i have 3 identical items of each item. //
      
      return recommandedProducts;
  };

}

샤 생크 비벡

아래와 같이 코드를 이동해보십시오.

export class ProductComponent implements OnInit {

  recommandedProducts: Product[];
  allProducts: Product[];
  product: Product = {type: "something" , id: 1};


  constructor(private productService: ProductService, private route: Router, private actRoute: ActivatedRoute) { }


  ngOnInit() {
    this.findRecommandedProducts(this.product)
  };

  findRecommandedProducts(currectProduct: Product){
    this.productService.getProducts().subscribe((data: Product[]) => {
      this.allProducts = data;
      this.recommandedProducts = this.allProducts.filter(otherProduct => 
      otherProduct.type == currectProduct.type && otherProduct.id == currectProduct.id) 
    });  
  };

}

이유:

을 사용하여 observable을 듣고 this.productService.getProducts()있으므로 this.allProducts비동기 적으로 채워 지므로에서 .filter작동하지 않습니다 undefined. 로 이동 subscribe그래서 지금 당신이 한 번, 블록 this.allProducts, 당신은 사용할 수 있습니다 .filter().

  1. 메이크업은 확실 this.product하다 defined주어진 코드에서 초기화 선언하지되었습니다.

  2. this.recommandedProducts = this.findRecommandedProducts(this.product)또한 작동하지 않습니다. 작업 방법에 대해 자세히 읽어보십시오 observable(이 작업 은 비동기 작업이므로 함수를 그런 방식으로 작성해야합니다).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Redux 앱 : 정의되지 않은 '필터'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 멀터의 속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 pls의 '필터'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 '커넥터'속성을 읽을 수 없습니다.

분류에서Dev

Apexcharts-ReactJs-실시간-정의되지 않은 '필터'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 '$ on'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 'then'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 속성을 읽을 수 없습니다.

분류에서Dev

입력 필드에 연결할 수 없음-정의되지 않은 '값'속성을 읽을 수 없습니다.

분류에서Dev

데이터를 가져올 수 없습니다. 처리되지 않은 거부 (TypeError) : 정의되지 않은 '0'속성을 읽을 수 없습니다.

분류에서Dev

인터셉터 TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

선택적 체인 수신 TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

정의되지 않은 discord.js의 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

React Hooks로 데이터 가져 오기-정의되지 않은 'map'속성을 읽을 수 없습니다.

분류에서Dev

이진 트리 코드-정의되지 않은 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

각도 데이터 테이블 : 오류 오류 : 정의되지 않은 'then'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다. discord.js

분류에서Dev

Highcharts 포맷터를 사용하면 정의되지 않은 '길이'속성을 읽을 수 없습니다.

분류에서Dev

반응 : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

분류에서Dev

데이터 맵이 정의되지 않은 'albersUsa'속성을 읽을 수 없습니다.

분류에서Dev

Uncaught TypeError : 정의되지 않은 'history'속성을 읽을 수 없습니다 !! React 라우터

분류에서Dev

TypeError : 정의되지 않은 '성공'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '고정'속성을 읽을 수 없습니다.

분류에서Dev

정보창-정의되지 않은 'open'속성을 읽을 수 없습니다.

분류에서Dev

.done ()은 정의되지 않은 속성을 읽을 수 없습니다.

분류에서Dev

Firebase 함수 : 정의되지 않은 https 속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '페이지'속성을 읽을 수 없습니다.

Related 관련 기사

  1. 1

    Redux 앱 : 정의되지 않은 '필터'속성을 읽을 수 없습니다.

  2. 2

    정의되지 않은 멀터의 속성을 읽을 수 없습니다.

  3. 3

    TypeError : 정의되지 않은 pls의 '필터'속성을 읽을 수 없습니다.

  4. 4

    정의되지 않은 '데이터'속성을 읽을 수 없습니다.

  5. 5

    정의되지 않은 '커넥터'속성을 읽을 수 없습니다.

  6. 6

    Apexcharts-ReactJs-실시간-정의되지 않은 '필터'속성을 읽을 수 없습니다.

  7. 7

    정의되지 않은 속성을 읽을 수 없습니다.

  8. 8

    정의되지 않은 '$ on'속성을 읽을 수 없습니다.

  9. 9

    정의되지 않은 'then'속성을 읽을 수 없습니다.

  10. 10

    정의되지 않은 속성을 읽을 수 없습니다.

  11. 11

    입력 필드에 연결할 수 없음-정의되지 않은 '값'속성을 읽을 수 없습니다.

  12. 12

    데이터를 가져올 수 없습니다. 처리되지 않은 거부 (TypeError) : 정의되지 않은 '0'속성을 읽을 수 없습니다.

  13. 13

    인터셉터 TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

  14. 14

    선택적 체인 수신 TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

  15. 15

    정의되지 않은 discord.js의 '데이터'속성을 읽을 수 없습니다.

  16. 16

    React Hooks로 데이터 가져 오기-정의되지 않은 'map'속성을 읽을 수 없습니다.

  17. 17

    이진 트리 코드-정의되지 않은 '데이터'속성을 읽을 수 없습니다.

  18. 18

    각도 데이터 테이블 : 오류 오류 : 정의되지 않은 'then'속성을 읽을 수 없습니다.

  19. 19

    TypeError : 정의되지 않은 '데이터'속성을 읽을 수 없습니다. discord.js

  20. 20

    Highcharts 포맷터를 사용하면 정의되지 않은 '길이'속성을 읽을 수 없습니다.

  21. 21

    반응 : 정의되지 않은 '데이터'속성을 읽을 수 없습니다.

  22. 22

    데이터 맵이 정의되지 않은 'albersUsa'속성을 읽을 수 없습니다.

  23. 23

    Uncaught TypeError : 정의되지 않은 'history'속성을 읽을 수 없습니다 !! React 라우터

  24. 24

    TypeError : 정의되지 않은 '성공'속성을 읽을 수 없습니다.

  25. 25

    TypeError : 정의되지 않은 '고정'속성을 읽을 수 없습니다.

  26. 26

    정보창-정의되지 않은 'open'속성을 읽을 수 없습니다.

  27. 27

    .done ()은 정의되지 않은 속성을 읽을 수 없습니다.

  28. 28

    Firebase 함수 : 정의되지 않은 https 속성을 읽을 수 없습니다.

  29. 29

    TypeError : 정의되지 않은 '페이지'속성을 읽을 수 없습니다.

뜨겁다태그

보관