2

I'm trying to get a response in json where I get an array which contains every declaration, with the user and category in it. I have it fixed statically, but I want to make it dynamic and can't seem to make it work.

EDIT: I'm statically inserting the 'id' in the method, so I'm only getting one declaration. I want to create some sort of loop where i can get back al the declarations which are on http://localhost:8080/declaraties. I've already tried nested http but no succes.

Code:

data.service.ts

    import { Injectable } from '@angular/core';
    import {Http} from '@angular/http';
    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/observable/forkJoin';
    import 'rxjs/add/observable/of';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/mergeMap';

@Injectable()
export class DataService {

  constructor(private http: Http) { }

  url = 'http://localhost:8080/declaraties/';

  getAllesVanDeclaraties(id: number): Observable<any> {
    return Observable.forkJoin([
      this.http.get('http://localhost:8080/declaraties/' + id).map(res => res.json()),
      this.http.get('http://localhost:8080/declaraties/' + id + '/gebruiker').map(res => res.json()),
      this.http.get('http://localhost:8080/declaraties/' + id + '/categorie').map(res => res.json())
    ])
      .map((data: any[]) => {
          const declaraties: any = data[0];
          const gebruiker: any[] = data[1];
          const categorie: any[] = data[2];
          declaraties.gebruiker = gebruiker;
          declaraties.categorie = categorie;
          return declaraties;
      });
  }
}

row.component.html

<ng-container *ngFor="let decl of data; let i = index;">

    <tr data-toggle="collapse" data-target="#demo1" class="accordion-toggle">
      <th>{{i + 1}}</th>

      <td>{{decl['gebruiker'].naam}}</td>
      <td>{{decl.datumIngediend}}</td>
      <td>{{decl.omschrijving}}</td>
      <td>{{decl['categorie'].naam}}</td>
      <td>&euro; {{decl.bedrag}}</td>

    </tr>......

row.component.ts

import {Component, OnInit} from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import {DataService} from './data.service';

@Component({
  selector: 'app-row',
  templateUrl: './row.component.html',
  styleUrls: ['./app.component.css']
})

export class RowComponent implements OnInit {

  data: any = [];

  constructor(private dataService: DataService) {
  }

  ngOnInit() {
    this.dataService.getAllesVanDeclaraties(2).subscribe( data => {
      console.log(data);
      this.data.push(data);
    });
  }
}

I've tried to make a nested http request, but I'm getting no responses.

3 Answers 3

1

try like this :

Observable.forkJoin([
    this.http.get('http://localhost:8080/declaraties/' + id),
    this.http.get('http://localhost:8080/declaraties/' + id + '/gebruiker'),
    this.http.get('http://localhost:8080/declaraties/' + id + '/categorie')
])
.subscribe((data: any) => {
    console.log('data[0]', data[0].json())
    console.log('data[1]', data[1].json())
    console.log('data[2]', data[2].json())
})
Sign up to request clarification or add additional context in comments.

Comments

1

I would first get all declaraties with:

this.http.get('http://localhost:8080/declaraties/').map(res =>{ 
  const declaraties: any[] = res.json();
  for(let i = 0; i < declaraties.length; i++){
    this.http.get('http://localhost:8080/declaraties/' + declaraties[i].id + '/gebruiker')
             .map(gebruiker =>{ declaraties[i].gebruiker = gebruiker.json(); })      
    this.http.get('http://localhost:8080/declaraties/' + declaraties[i].id + '/categorie')
             .map(categorie=>{ declaraties[i].categorie = categorie.json(); })
  }
  return declaraties;
})

1 Comment

I’m now getting an object as return value. How can I convert this to an array? Also, I want to have an array with al declaraties, and Inside the declaraties I also want to have the gebruiker array nested. Any idea how I can achieve this? The standard json object that Spring provides has the gebruiker url nested inside, so I probably have to do a double subscribe of some sort..
0

You can create a wrapper method that can handle an array of ids. This method can call your current getAllesVanDeclaraties method to build out http requests for each id

// data.service.ts
getAllesArray(ids: number[]){
    // build out array of observables
    let declarations = ids.map(id => this.getAllesVanDeclaraties(id));

    // call observables in parallel
    return Observable.forkJoin(...declarations);
}

Now when you subscribe to this in your component, you'll get data for multiple ids

// component.ts
this.getAllesArray([1, 2]).subscribe(res => {
    // res[0] is the result from id = 1
    // res[1] is the result from id = 2
});

Comments

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.