Search code examples
angularangular-servicessubscription

Passing a parameter to a method inside ngOnInit doesnt even call a Service, log nothing


On a previous component page I made once u click on a button it takes u to some other component using navigation. Considering I had to write two parameters, one for a current path and second for the next, when I try to subscribe to it, it gives me an Object with 2 elements.

onSelect(subCategory) {
    this.router.navigateByUrl(
      `web-shop/categories/${this.type}/${subCategory.nextType}`
    );}

In ngOnInit I caught that object, changed it into an array and gave its value to another variable. Then I took the second element of the array. When I called a method inside this.products as following down in the code, and filtered it, then logged it, I get nothing in the console.log, even tho I am passing the actual value I need, and I checked to see if the value is what I need. But for some reason I get nothing for console.log for the this.products, not even an array or a value.

import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute } from "@angular/router";
import { Subscription } from "rxjs";
import { SingleProductModel } from "./../../Models/webshopModels/single-product.model";
import { SingleProductService } from "./../../Services/webshopServices/single-product.service";

@Component({
  selector: "app-products",
  templateUrl: "./products.component.html",
  styleUrls: ["./products.component.scss"],
})
export class ProductsComponent implements OnInit {
  type: string;
  products: SingleProductModel[];
  s: string;
  type1: Array<any>;
  constructor(
    private productsService: SingleProductService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.route.params.subscribe((data) => {
      this.type1 = Object.values(data);
      console.log(this.type1);
      this.type = this.type1[1];
      console.log(this.type);
      this.products = this.productsService
        .getCategoryProducts()
        .filter((a) => (a.type = this.type));
      console.log(this.products);
    });
  }
}

And I even tried to filter it inside the service itself. Not in the example I sent.

  getCategoryProducts() {
    return this.product.slice();
  }

Solution

  • So I solved the problem. Issue was, I had a method in the model, which caused my program to not work and break, because I called a function from a product-review service inside the product service, and this method that broke the code was inside product-review model. I tried something and it was stupid. Thanks for the help all.