1

the data of api is array but the response i am getting from it is as an object using following method

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-shipment',
  templateUrl: './shipment.component.html',
  styleUrls: ['./shipment.component.css'],
})
export class ShipmentComponent implements OnInit {
  li: any;
  shipment = [];
  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    this.http.get('http://localhost:3000/shipments').subscribe((response) => {
      this.li = response;
    });
  }
  getShipments(): void {
    console.log(this.li);
  }
}

1 Answer 1

2

By default, the type returned by an http get request is Observable<Object> so you have to tell typescript that you expect an array. This can be simply done by typecating

  ngOnInit(): void {
    this.http.get<any[]>('http://localhost:3000/shipments').subscribe((response) => {
      this.li = response;
    });
  }

OR

  import { tap, map } from 'rxjs/operators';

  ngOnInit(): void {
    this.http.get('http://localhost:3000/shipments')
      .pipe(
         tap(console.log),
         map(x => x as any[])
       )
      .subscribe((response) => {
         this.li = response;
       });
  }

the line tap(console.log), is just for debugging to view the object returned from your api call

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please provide insight to ... Found one library(exceljs) which supports this...stackoverflow.com/questions/67130632/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.