0

I would like to get data with a subject from http. I have a service (storage.Service) that "call" the http connection with the subject and copy the array to another service (myinfo.Service) that will pass the data to the page.ts

I arrive to get the array and pass it to myinfo.service, but i don't arrive to copy it, because after i call the metode to copy the info.service array to the page.ts th array is empty.

Thanks so much for your help.

info.service code:

 private myinfoData: MyinfoModel[] = [];

  myinfoSubject = new Subject<MyinfoModel[]>();


  constructor() { }

  setInfo(myinfoData: MyinfoModel[]) {
    this.myinfoData = myinfoData;
    this.myinfoSubject.next(this.myinfoData.slice());
  }

  getInfo() {
    return this.myinfoData.slice();
  }

storage.service code:

  private _url: any = '../assets/DB-Test/DB_MyInfo.json';


  constructor(private http: HttpClient, private myinfoService: MyinfoService) { }

  getMyInfo() {
    this.http.get<any[]>(this._url).subscribe(
      (response: MyinfoModel[]) => {
        const infoData: MyinfoModel[] = response['DB_Portfolio']['TB_MyInfo'];
        this.myinfoService.setInfo(infoData);
        console.log(response);
        console.log(infoData);
      },
      (error) => {
        console.log('Error: ' + error);
      }
    );
  }

Page.ts code:

  infoDataBis: MyinfoModel[];

  constructor(private dbStorage: DbStorageService, private myinfoService: MyinfoService) { 
    this.dbStorage.getMyInfo();
  }


  ngOnInit() {
    this.infoDataBis = this.myinfoService.getInfo();
    console.log(this.infoDataBis);

on the console.log I've wrote in the storage.service

        console.log(response);
        console.log(infoData);

I see the arrays

on the console.log I've wrote in the page.ts

   console.log(this.infoDataBis);

the array is empty

When i debug i see the array in the info.service getting some that during the "call" of the storage.service then it get empty. So when i call the method getInfo() i copy an empty array.

With this logique, i thought i could copy the data to a service that i can re-use in any page.ts i want later. But i don't arrived to do it :(

Thanks for your help!!

JBD
  • 568
  • 8
  • 25
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/1260204) – Igor May 20 '19 at 15:44
  • Although the 2nd mentioned duplicate ([How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/1260204)) surely applies it might also be both. It is critical that you understand how to work with asynchronous calls in javascript, and by extension typescript. Understanding these core concepts will help you become a better programmer and also ensure you do not keep "stubbing your toe" on the same problem. – Igor May 20 '19 at 15:46
  • @Igor When you have a good experience on it it can help. But when you learning like me, it's speaking like chinese. – JBD May 20 '19 at 15:56
  • So if someone can answer if it knows how to do it, it will be nice for me to understand how it works and where is my mistake. – JBD May 20 '19 at 15:57
  • Which is why it is critical to understand the basics of asynchronous programming. Read through the answer found in [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/1260204) again. Understand how to work with async calls and use the responses and then read your code again. You will easily find your mistakes. – Igor May 20 '19 at 15:59
  • @Igor They don't talk about Subject and about asObservable as well. So if you don't want to answer with a the correct code, don't answer anymore on this post please. I'm losing my time. Have a good evening Igor – JBD May 20 '19 at 16:21

0 Answers0