1

I am trying to get value of a token but I get error ERROR TypeError: Cannot read property 'token' of undefined

enter image description here

code

//Below code gives the output shown below with black color text
data.text()

// I am interested in fetching token value which is nested inside success keyword, below code fails to get the token
var abc = data.text();
abc['success'].token
Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93
  • It seams that `abc` is a `String`, not an `Object`. So `abc['success']` doesn't fit the need (it's null). Maybe you should use JSON's parser or change the type of returned data. –  Oct 22 '17 at 12:59

3 Answers3

2
let abc = JSON.parse(data.text());
1
var abc = (JSON.parse(data._body)).success.token;
Miguel Coder
  • 1,896
  • 19
  • 36
0

Following code is for reading JWT form js

function parseJwt (token) {
            var base64Url = token.split('.')[1];
            var base64 = base64Url.replace('-', '+').replace('_', '/');
            return JSON.parse(window.atob(base64));
        };

How to decode jwt token in javascript

Nisal Edu
  • 7,237
  • 4
  • 28
  • 34