1
votes

I developp a app with Angular 9, and I want return an array with my response. In my service class I have this method,

 getReport(startDate: string, endDate: string) {
    return this.http.get<ReportReport>(
      `https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`,
      this.httpOptions);
}

My object return is Report

export interface Report {
  range: string;
  opens: number;
  closes: number;
}

And I would like getReport return an array like this.

 list= [
    {
      name: 'Opens',
      value: Report.opens,
      color: 'green',
    },
    {
      name: 'Closes',
      value: Report.closes,
      color: 'red',
    }
]

So i tried with a subscribe observable like :

 getReport(startDate: string, endDate: string) {
     return this.http.get<ReportReport>(
      `https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`,
      this.httpOptions).subscribe(result => { return [{
        name: 'Opens',
        value: result.opens,
        color: 'green',
      },{

        name: 'Closes',
        value: result.closes,
        color: 'red',
      }]
    });
}

with this method I have a subscription return.

and with a map

 getReport(startDate: string, endDate: string) {
     return this.http.get<ReportReport>(
      `https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`,
      this.httpOptions).pipe(map(result => { return [{
        name: 'Opens',
        value: result.opens,
        color: 'green',
      },{

        name: 'Closes',
        value: result.closes,
        color: 'red',
      }]
    }));
}

but its doesn't work with subscribe and map.

with this method I have a Observable<{name: string; value: number color: string;}[]> for the return I just i would like this return -> {name: string; value: number color: string;}[] not observable

So it's possible to do that ? or/and How I can make this ?

Thank you for your help

2
No, it is not possible. You can't convert an asynchronous value to synchronous. You could make it into a promise and use async/await, but rather than that, embrace the observables. They are one of the best parts of Angular. - Akxe
thank you for you response, sorry but i'm start with angular so I don't know make this. How i can ? - Heptagram
Why you do not want to return Observable<{name: string; value: number color: string;}[]> ? - Stefan
because i want initialize an array for my component and use a ngfor for display data - Heptagram
Use of: this.httpOptions).pipe(map(result => { return of([{ ... - developer033

2 Answers

0
votes

You could subscribe to the HTTP Observable in the controller and define your array there. Try the following

Service

getReport(startDate: string, endDate: string) {
  return this.http.get<ReportReport>(`https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`, this.httpOptions);
}

Controller

public report: any;

this.reportService.getReport('...', '...').subscribe(
  response => {
    this.report = [
      {
         name: 'Opens',
         value: result.opens,
         color: 'green',
      },
      {
         name: 'Closes',
         value: result.closes,
         color: 'red',
      }
    ];
  },
  error => { // handle error }
);

And use it in the template

<ng-container *ngIf="report">
  <div *ngFor="let item of report"> 
    {{ item.name }}
    {{ item.value }}
    {{ item.color }}
  </div>
<ng-container>
0
votes

I tried to use an async/await with promise

  async getReport(startDate: string, endDate: string){
    return await this.http
      .get<Report>(
        `https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`,
        this.httpOptions
      )
      .toPromise();
  }
getReports(startDate: string, endDate: string) {
    this.getReport('2020-03-20', '2020-03-26').then(response => {
      let lists= [
       {
        name: 'Opens',
        value: response .opens,
        color: 'green',
      },
        {
        name: 'Opens',
        value: response .opens,
        color: 'green',
      }];
    });

its possible to use the list outside the promise ? exemple if i want return list from my method getReport ?