@Component({
selector: 'app-product-form',
templateUrl: './product-form.component.html',
styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
categories$;
product:Product = {category:"", price:0, imageUrl:"",title:""};
product2;
id;
constructor(
private router:Router,
categoryService:CategoryService,
private productService:ProductService,
private route:ActivatedRoute) {
this.categories$ = categoryService.getCategories().snapshotChanges();
this.id = this.route.snapshot.paramMap.get('id');
if(this.id) {
this.productService
.getOneProduct(this.id)
.snapshotChanges()
.pipe( take(1) )
.subscribe( p => this.product2 = p.payload.val());
if(this.product2){
this.product.category = this.product2.category;
this.product.price = this.product2.price;
this.product.imageUrl = this.product2.imageUrl;
this.product.title = this.product2.title;
}
}
export interface Product{
price:number;
title:string;
category:string;
imageUrl:string;
}
I want to instantiate product.title, product.category, and so on , but since product2 is instantiated by a async request i am not able to do so, how can i fix this issue. if(this.product2) block's code is never executed due to the async request.