1

I'm new to angular and ionic and i have to develop an application that can connect to a backend and get users data. The application is able to extract user data, but not to save them in a variable with subscribe method.

The PeopleService:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ReplaySubject } from 'rxjs';
import {map} from 'rxjs/operators';


@Injectable({
  providedIn: 'root',

})


export class PeopleService {

      public activeProject:ReplaySubject<any> = new ReplaySubject(1);
      public people: any;

  constructor(public http: HttpClient ){
    console.log("Ciaooo");
  }

  getPeople() {

        this.http.get('.....................').pipe(map(res => res)).subscribe(res => this.activeProject.next(res));
        return this.activeProject;
  }



}

Home:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
import { Component, OnInit } from '@angular/core';
import {PeopleService} from '../../people-service.service';
import { ReplaySubject } from 'rxjs';


console.log("ouu");
@Component({
  selector: 'app-start-p',
  templateUrl: './start-p.page.html',
  styleUrls: ['./start-p.page.scss'],
  providers: [PeopleService]
})

export class StartPPage implements OnInit {
  //public people : any;

   public people : any;



  constructor(public peopleService : PeopleService){
    console.log("Start p page costruito richiamo loadPeople");
  }

  ngOnInit() {


    this.people = null;
    console.log("loadPeople avviato, provo a inviare richiesta pacchetti a peopleService.getPeople");
    this.peopleService.getPeople();

 
    this.peopleService.activeProject.subscribe(active => {this.people = active, console.log(active)});
    console.log(this.people);
  }

}

The return JSON is like this:

{"people": [{"gender":"female","name":{"title":"miss","first":"willemien","last":"heslinga"},"email":"[email protected]"},{"gender" : "male", "name":{"title":"monsieur","first":"bernard","last":"olivier"},"email":"[email protected]"}]}

My app can read the json file, but can't save it. Runs the console.log but fails to put active in this.people

1
  • If you look at Parent and children communicate via a service as an example, you'll see that you wouldn't subscribe directly to a Subject<T>. You would instead expose an observable (asObservable()) based on the subject from the service and subscribe that to that instead. Also, why even use a subject in your example, why not just consume the data of getPeople() directly? Commented Feb 21, 2019 at 22:42

1 Answer 1

1

I would try restructuring this set up a little to bit to keep it simple and get it working. As mention in the comment lets work on consuming getPeople() directly. Looking like the following. Be sure to import Observable from RxJS.

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

private getPeople():Observable<any> {
 return this.http.get(//filler details//).pipe(map(data => data)
 );
}

Notice the function above is set to return an `Observable' which means in your component you can subscribe to it directly looking like so.

this.peopleService.getPeople().subscribe(data => {
  console.log(data); // is good, should show you people.
}, error => {
  console.log(error); // HttpError will provide error and type.
});

This removes the complicated logic involving the subject while giving you the required functionality. Any issues drop a comment. BR.

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

2 Comments

Thankssss, I wrote: this.peopleService.getPeople().subscribe(data => { console.log(data), this.people = data.people; // It Works!! }, error => { console.log(error); // HttpError will provide error and type. });
Glad it helped :- )

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.