Angular HTTP get返回空数组

时间:2018-09-20 03:20:39

标签: angular http

要更新数据,我必须调用getProduct(),它返回一个空数组

getProduct() {
        return this.httpObj.get(`${this.uri}/products`);
    }

下面是updateproduct.ts的ngonit():

ngOnInit() {
       this.router.params.subscribe(params =>{
          this.id=+params['id'];
       });
       this.productService.getProduct().subscribe((data:Response)=>this.products=data.json());
       console.log(this.data)
  //console displays empty array

  }

2 个答案:

答案 0 :(得分:0)

请执行此操作,在您的订阅中控制台记录

ngOnInit() {
       this.router.params.subscribe(params =>{
          this.id=+params['id'];
       });

      this.productService.getProduct().subscribe((data:Response)=>{
           this.products=data.json();
           console.log(this.products)
      });
 }

答案 1 :(得分:0)

**首先,请检查您得到的响应,并且最好在实施之前先通过Postman或rest客户端检查API。 **

     ngOnInit() {
               this.router.params.subscribe(params =>{
                  this.id=+params['id'];
               });
               this.productService.getProduct().subscribe(
    (data:Response)=>
this.products=data.json()
     console.log("responsedata",data)
    );

          }