0

I want to use data from the axios response. I know it returns a promise object, but how do I manipulate the data in .then, and make it accessible? Do I need to add a callback function to my getData method? I want the properties to be accessible and stored in a variable outside the axios request, and in another class, and not just console.log.

I want to store variables from data.response.name etc. This is what I have tried so far, but I get undefined or promise: Object

export class myClass {

  public async getData() {

     return axios.get(url)
       .then(response => response.data)
       .catch((error) => {
          console.log(error);
        });

   }


    public async getName() {
       return this.getData().then((res:any) => {
          return res.name;
       });
    }
}

Outside class:

const myData = new myClass().getName().then(res => res);

**I have update my question, and tried to do the answer as described here, but i still get undefined:* How to access the value of a promise?

public async getData() {
        const promiseB = this.promiseA().then(function (result) {
            return result;
        });

        promiseB.then(function (result) {
            // here you can use the result of promiseB
            result = result.name;
            return result;
        });
    }
user6086008
  • 101
  • 3
  • 14
  • Currently you access `response.data.data.name` ... Maybe you want to remove one of the `.data`s ? – Jonas Wilms Jun 20 '19 at 23:02
  • ok, have updated to `return res.name`, but i still get undefined when trying to use the value. The console.log prints ok, but thats not what i want, i want to use the value. – user6086008 Jun 20 '19 at 23:25
  • 2
    `myData` will always be a promise, never the value you want, not even the NOP pattern `.then(res => res);` will help – Jaromanda X Jun 20 '19 at 23:25
  • you say you want `data.response.name` ... but you are getting `data.name` from the `response` – Jaromanda X Jun 20 '19 at 23:27
  • yes, based on comments above `res.name ` in `getName`, will actually return `response.data.name` from the response in `getData` – user6086008 Jun 20 '19 at 23:32
  • @Jaromanda X i understand it returns a promise, but is there not a way to set the data, so then properties of the object can be returned? – user6086008 Jun 20 '19 at 23:34
  • Have you tried waiting for the promise to resolve and return that value instead of just returning the promise immediately? `return await axios.get(...` – Drew Reese Jun 20 '19 at 23:57
  • @JonasWilms, I have updated my answer, and tried to use your linked example, but still doesnt work for me. Would you mind pointing me in the right direction? – user6086008 Jun 21 '19 at 11:20
  • Now you don't `return` anything from `getData`. – Jonas Wilms Jun 21 '19 at 11:22
  • @JonasWilms ok, so i should return `promiseB` from `getData`? – user6086008 Jun 21 '19 at 11:33
  • Yes. But why do you need `promiseB` at all? `return this.promiseA().then(result => result.name)` is enough. – Jonas Wilms Jun 21 '19 at 12:09
  • @JonasWilms ok, i tried that. But this does not seem to solve the issue of being able to access the value. I still get `Promise { } ` when trying to use `getData`. I can only get the value from `console.log`, which is not what i want. – user6086008 Jun 21 '19 at 12:27
  • Have you tried `getData().then(console.log)` ? – Jonas Wilms Jun 21 '19 at 12:45
  • @JonasWilms, but that just results in a console.log, im still not able to use the value of it. – user6086008 Jun 23 '19 at 11:49

0 Answers0