# Get data in subscribe

**URL:** <https://forum.ionicframework.com/t/get-data-in-subscribe/96501>\
**Category:** ionic-v3\
**Created:** [June 30, 2017, 10:17pm UTC](https://forum.ionicframework.com/t/get-data-in-subscribe/96501 "2017-06-30T22:17:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafaelmoura](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rafaelmoura/32/106057_2.png) [@rafaelmoura](https://forum.ionicframework.com/u/rafaelmoura)\
**Post date:** [June 30, 2017, 10:17pm UTC](https://forum.ionicframework.com/t/get-data-in-subscribe/96501/1 "2017-06-30T22:17:17Z")

</div>

Why

```auto
  test: any[];

  constructor(public navCtrl: NavController, public navParams: NavParams, public service: ServiceProvider, public loadingCtrl: LoadingController) {
    this.method1();
  }

  method1() {
    this.service.getData()
        .subscribe(
          data => this.test= data,
          e => console.log(e)
        );
  }

```

I’m trying get “this.test” in method2

```auto
method2() {
  this.otherList= [];
  console.log(this.test);
  // for(let t of this.test) {
  // console.log(t);
  // }
}

```

Why this.test to show in console undefined ?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 30, 2017, 10:48pm UTC](https://forum.ionicframework.com/t/get-data-in-subscribe/96501/2 "2017-06-30T22:48:40Z")

</div>

Given only what you’ve told us so far, all I can say is “because you called `method2()` before `getData()` resolved”.

You should do your best to design things so that you don’t have these sorts of situations. `method2()` needs to be able to function regardless of whether `this.test` has been populated yet, or its functionality can be folded into the subscription of `method1()`, or it can to do its own subscription. Explicitly attempting to synchronize (such as with semaphores) is sort of antithetical to how asynchronous JavaScript programming is intended to be written.

My general design pattern here is to have service providers expose somehow (either as properties or return values of methods) `Observable`s of business object interfaces. Pages inject these providers, subscribe to them either at construction or using lifecycle events, and update their own properties accordingly. All other page methods and templates should be written so as not to particularly care whether these page properties derived from provider data are blank/dummy, loaded, or modified while they are not paying attention.

---

<div class="post-metadata">

**Author:** ![ralphskie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ralphskie/32/110925_2.png) [@ralphskie](https://forum.ionicframework.com/u/ralphskie)\
**Post date:** [July 1, 2017, 1:18am UTC](https://forum.ionicframework.com/t/get-data-in-subscribe/96501/3 "2017-07-01T01:18:59Z")

</div>

Your using both Synchronous(method 2) & Async(method 1).

The reason why you cannot get “this.test” even when you write your code like this is because:

```auto
method1();
method2();

```

it will run the method1 and method 2  
method 2 will finish first but method1 is not yet finish.  
that is why you have the “subscribe” in method1 to observe if there is a result.

As you can see you are dealing with an observable (method 1).  
The best way to get the data pass to the “this.test” is put the “method 2” inside the observable, like this

```auto
method1().subscribe(
    this.test = data;
    //call the synchronous method here
    this.method2();
);

```

a good article to have an idea with sync/async is this : [https://www.joshmorony.com/dealing-with-asynchronous-code-in-ionic/](https://www.joshmorony.com/dealing-with-asynchronous-code-in-ionic/)

---

<div class="post-metadata">

**Author:** ![rafaelmoura](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rafaelmoura/32/106057_2.png) [@rafaelmoura](https://forum.ionicframework.com/u/rafaelmoura)\
**Post date:** [July 1, 2017, 11:53am UTC](https://forum.ionicframework.com/t/get-data-in-subscribe/96501/4 "2017-07-01T11:53:42Z")

</div>

Thanks for your help, I did it!  
I was using

```auto
  method1() {
    this.service.getData()
         .subscribe(
           data => this.test = data,
           e => console.log(e)
         );
  }

```

This not work, tnks my friend!
