Marcin Żmigrodzki
Marcin Żmigrodzki

Reputation: 393

How to get values of two observables at a time which listen to http.get?

I have written small application which gets data from two tables in mySQL via PHP pages: getRow.php and getColumn.php.

I successfully read content of those tables if done separately (when I comment on or another), but when I try to get them both there is an error 'Cannot read property '1' of undefined'.

As a result values of thoes arrays are blank. Suprisingly the length of those arrays seems correct.

ngOnIinit listens to the values of those arrays. getRow and getColumn collects values from mySQL via php pages as mentioned.

export class Row {
public id: number;
public title: string;
public sequence: number;
constructor(
  id: number,
  title: string,
  sequence: number) {
    this.id = id;
    this.title = title;
    this.sequence = sequence;
  }
  }

rows: Row[];
row: Row;
columns: Row[];  
column: Row[];


ngOnInit() {       
 forkJoin(this.DbService.getRow(this.idBoard),
    this.DbService.getColumn(this.idBoard)
    ).subscribe(val => {

      this.rows = val[0].slice();
      this.columns = val[1].slice();
      this.rowNumber = this.rows.length;
      this.columnNumber = this.columns.length;
    });
}

If I comment the line 'this.columns = val[1].slice();' it works fine but only one array. The similar behaviour if I comment the line 'this.rows = val[0].slice();'.

getRow(): Observable<Row[]> {
  return this.http.get(`${this.baseUrl}/getrow`).pipe(
    map((res) => {

        this.rows = res['data'];
        return this.rows;
    }),
    catchError(this.handleError));
}

getColumn(): Observable<Row[]> {
  return this.http.get(`${this.baseUrl}/getcolumn`).pipe(
    map((res) => {

        this.rows = res['data'];
        return this.rows;
    }),
    catchError(this.handleError));
}

As a result I would like to have rows[] and columns[] filled with proper values.

Upvotes: 0

Views: 45

Answers (1)

Charly Sosa
Charly Sosa

Reputation: 575

Try this in your forkjoin:

  this.rows = val[0]['data'].slice();
  this.columns = val[1]['data'].slice();

Regards

Upvotes: 1

Related Questions