【问题标题】:Cannot find property 'filter' of undefined [duplicate]找不到未定义的属性“过滤器”[重复]
【发布时间】:2020-07-09 06:55:40
【问题描述】:

我已经建立了我的在线商店项目 (Angular) 并尝试在每个产品页面中创建某种“推荐”区域。

起初我想订阅我所有的产品,然后查看它们以找到匹配的类型和 ID。

只是为了测试,我在我的 MongoDB 中创建了 18 个产品(6 个不同的产品)(3 个具有相同类型和 id 的相同产品的产品),但不幸收到此错误并且无法弄清楚原因:

无法读取未定义的属性“过滤器” 在 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;
  };

}

【问题讨论】:

  • 你好摩西,欢迎来到 StackOverflow!您需要在 .subscribe() 代码块内进行过滤。那是您确定您已从后端/服务/数据提供商那里收到答复的地方。

标签: javascript angular typescript rxjs


【解决方案1】:

尝试如下移动代码:

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) 
    });  
  };

}

原因:

您正在使用 this.productService.getProducts() 收听 observable,因此您的 this.allProducts 将被异步填充,因此 .filter 将无法在 undefined 上运行。将它移动到 subscribe 块,所以现在一旦你有了 this.allProducts ,你就可以使用 .filter()

  1. 确保 this.productdefined ,在给定的代码中它已被声明且未初始化。

  2. this.recommandedProducts = this.findRecommandedProducts(this.product) 也不起作用。请阅读更多关于使用observable 的方式(这些是异步操作,因此您的函数必须以这种方式编写)

【讨论】:

  • 感谢您的帮助!它确实有帮助,但现在我收到此错误:错误类型错误:无法读取未定义的属性“类型”:otherProduct.type == currectProduct.type && otherProduct.id == currectProduct.id)
  • @moses:正如我提到的,你需要初始化 product: Product = {type: "something" , id: 1} 。参考点号1
  • 我在我的 Product Schema、Product.ts 文件和我的数据库中这样做了,你可以看到它们显示在我的首页:imgur.com/D64k6y3
  • @moses :不,您需要在打字稿的class 内的组件级别上执行此操作。您能按照我新更改的代码中的说明进行更改吗?做相应的更改,并让我知道错误已得到修复。同时,您如何初始化product.ts 中的值并在product-component.ts 中使用它。分享product.ts的代码
  • 非常感谢您的帮助,但我发现问题在于使用 rxjs 过滤器过滤可观察对象,但我没能做到......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 2021-05-06
  • 2014-09-30
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2017-10-15
相关资源
最近更新 更多