0

easy question:

 FunctionOutput: Promise {
  _c: 
   [ { promise: [Object],
       resolve: [Function],
       reject: [Function],
       ok: [Function],
       fail: [Function],
       domain: null } ],
  _a: undefined,
  _s: 1,
  _d: true,
  _v: 
   { body: 
      { token_type: 'bearer',
        access_token: 'token',
        expires_in: 7776000,
        refresh_token: 'token' },
     statusCode: 200 },
  _h: 0,
  _n: true }

This is my Output from a function and I want to specify output "access_token" How do I do that?

console.log("token is"+ data._v.body.access_token);

does not work...

Pls help Thanks a lot!

  • 1
    Please add a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve/) which shows the actual problem. – Andreas Jun 01 '18 at 12:13
  • How did you assign the object to `data`? It might very well be that you assigned a *promise* to `data`, not the promised value, which can only be retrieved asynchronously. – trincot Jun 01 '18 at 12:13
  • have you debugged to into the code to see if data, _v and body are populated with values or are undefined? Which part "does not work"? – daddygames Jun 01 '18 at 12:14
  • 1
    How are you getting `data`? – T.J. Crowder Jun 01 '18 at 12:15
  • What's the output you're getting from `console.log("token is"+ data._v.body.access_token);`? is it undefined? are you getting an error? please explain more, so we can help – Bentaiba Miled Basma Jun 01 '18 at 23:21

2 Answers2

0

What you've shown is a promise. You'd use the promise via its then method:

data
.then(function(result) {
    // Use result here
})
.catch(function(err) {
    // Handle error here
});

We can't tell you how to access access_token on the result, because we don't know what part of what you've shown (if any) will be the resolution value. It may be result.access_token, or result.body.access_token. But you won't be able to access it except in a then callback.

data
.then(function(result) {
    console.log(result.body.access_token);
})
.catch(function(err) {
    // Handle error here
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can use destructuring if you just want to have the access_token

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

// whatever you're calling that returns that object
const mockRequest = () => new Promise(resolve => resolve(res))
// response
const res = {
  body: {
    token_type: 'bearer',
    access_token: 'token',
    expires_in: 7776000,
    refresh_token: 'token'
  },
  statusCode: 200
}

/*
  calls async function it then waits until its 
  finsihed the request and "then" calls the then with the data

  Normally we would just return what ever comes back
  i.e (data) => data.body.access_token
  But we can use a new ES6 feature which just returns the object
  name passed instead 
  i.e ({body}) => { token_type: 'bearer' ... 
*/

function getAccess() {
  mockRequest()
    .then(({body: {access_token}}) => console.log(access_token))
    .catch(err => console.log(err))
}

getAccess();

/*
  // Or using es7 features such as async await
  async function getAccessES7() {
    const {body:{access_token}} = await mockRequest();
    return access_token;
  }
  getAccessES7();
*/
Joe Warner
  • 3,335
  • 1
  • 14
  • 41