0

I dont know why the code is not running. Even the console.log('1') never appears.

  ngOnInit() {
    this.getContacts();
    this.getClients();
  }

  getContacts(): Observable<any[]> {
    return this.http.get('/api/contacts')
      .map((res: any) => res.json()).flatMap((contacts: any[]) => {
        if (contacts.length > 0) {
          console.log(1);
          const observables = Observable.forkJoin(
            contacts.map((contact: any) => {
              return this.http.get('/api/client/' + contact.company_name)
                .map((res: any) => {
                  let company_name: any = res.json();
                  contact.company_name = name;
                  console.log(contact.company_name);
                  return contact;
                });
            })
          );
          console.log(observables);
          return observables;
        }
        return Observable.of([]);
      }
    );
  }

The plain response of localhost:4200/api/contacts is:

[{"_id":"59f43f363a2fc421c00ad8b2","anrede":"sir","titel":"dr.","vorname":"name1","nachname":"surname1","company":"59f43f0e3a2fc421c00ad8b0","__v":0},{"_id":"59f43f443a2fc421c00ad8b3","anrede":"mrs","titel":"prof.","vorname":"name2","nachname":"surname2","company":"59f43f1a3a2fc421c00ad8b1","__v":0}]

The plain response of localhost:4200/api/client is:

{"_id":"59f43f1a3a2fc421c00ad8b1","name":"company2","street":"street 2","zipcode":"45678","city":"city2","__v":0}

I followed this

hevilp
  • 57
  • 1
  • 1
  • 7
  • 2
    You never *subscribe* to the observable `getContacts` returns... – jonrsharpe Oct 30 '17 at 17:08
  • I searched deep in the questions. The topic is duplicated but the first questions title is realy bad, that you will never find it with any of my key words. The title should be edited like mine. I got banned for this.... – hevilp Nov 01 '17 at 19:51
  • Why does the title need to be edited? Now this one is a signpost to the same information. Which was in the framework docs all along anyway; you could have just not asked the question. – jonrsharpe Nov 01 '17 at 19:54
  • I already said it, the author didnt used the important keyword "observables", which is very important... – hevilp Nov 01 '17 at 19:58
  • Yes, and now you have used that word, and the duplicate banner links visitors through. *That's the point of flagging duplicates.* See e.g. https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – jonrsharpe Nov 01 '17 at 19:59
  • And I am banned because of this question. – hevilp Nov 01 '17 at 20:00
  • That's not how bans work. If you have any further issues, see the [help] or [meta]. – jonrsharpe Nov 01 '17 at 20:02

1 Answers1

2

In order for the http module to make the rest call you must add subscribe to it.

In your case this would be :

this.getContacts().subscribe((data) => {//do something with data})
Niles Tanner
  • 3,911
  • 2
  • 17
  • 29