0

I am doing an api call that returns a promise. The call works fine but I want to do treat data contained within the promise. Here is My call:

  let promiseArray = this.get('store').query('member', {query: term, option: this.get('option')});
  promiseArray.then(members  => {console.log(members);
  });

  let var= members;
  console.log(var);

My problem is that this doesn't return an array of my model i.e members, also the second display of members display undefined , it return an object containing a lot of meta data, also the array but inside some meta data.

How could I get simply the array ?

Aymen Ragoubi
  • 298
  • 5
  • 22
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – sinanspd Mar 18 '20 at 19:17
  • 1
    What you need to do is inside the then function. – Sajeeb Ahamed Mar 18 '20 at 19:19
  • I want to get the array members in order to do same treatment, but instead I am getting an object containing a lot of meta data and within them I can find somewhere my array – Aymen Ragoubi Mar 18 '20 at 19:22

1 Answers1

1

You can use async await for your purpose.

const promiseFunc =  () => {
    // Return the promise and await this inside a async function
    return this.get('store').query('member', {query: term, option: this.get('option')});
}

const asyncFunc = async () => {
    const value = await promiseFunc();
    console.log(value);
}

asyncFunc();
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
  • If you console outside of the async function then that should be undefined. You have to do whatever you want to do inside the async function. – Sajeeb Ahamed Mar 18 '20 at 19:40
  • not outside, inside the assync function I am getting undefined – Aymen Ragoubi Mar 18 '20 at 19:48
  • I forgot the return, it works but I still getting an object containing a lot of meta data not just an array of data as returned from back office – Aymen Ragoubi Mar 18 '20 at 21:42
  • Could you please share the output you get? – Sajeeb Ahamed Mar 19 '20 at 05:24
  • 1
    Ember data adds meta data - this isn't bad, just record keeping stuff gernaly, you can just ignore it any operations you run (map, filter, forEach) will be run on the original data. You can also call .toArray() to get only the models out. – jrjohnson Mar 20 '20 at 06:57