1

So basically I'm requesting a post request to the server which in return, send a token in JSON object in this form:

{
   token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIyLCJpc3MiOiJodHRwczovL2hvYnVkZGllcy5jb20vYXBpL2F1dGgvbG9naW5fYXBpIiwiaWF0IjoxNTE3MzkwNTkwLCJleHAiOjE1MTczOTQxOTAsIm5iZiI6MTUxNzM5MDU5MCwianRpIjoiWUxtdXJ5TkV2UzlwSEExTyJ9.nWV7OWLYdMJmZCHNP9tFuAmZ84DwzYO00O3jQ_RfhXQ"
}

What I required is to append that token with the apiUrl to get the appropriate information of a user currently logged in. The server is returning this JSON object in a variable "result" as shown below:

// for login the user in the app.
 loginUser() {
//values from the login form
var data = {
  email: this.emailVar,
  password: this.passwordVar
}
//calling the loginUser method from the Provider: Test.ts
this.restProvider.loginUser(data)
  .then((result) => {
    //storing the returned Token from the server.
    var login_token = result;
    //storing the retured Token into Ionic Storage.
    this.storage.set("tokenn", result);
    console.log(result);
    console.log('success');
    //if successful, then set the rootPage to TabsPage
    this.navCtrl.setRoot(TabsPage);
  }, (err) => {
    console.log(err);
  });

But when I'm appending the result, stored in the storage, It's appending that retured object, wehere I only need the string.

I only required this:

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIyLCJpc3MiOiJodHRwczovL2hvYnVkZGllcy5jb20vYXBpL2F1dGgvbG9naW5fYXBpIiwiaWF0IjoxNTE3MzkwNTkwLCJleHAiOjE1MTczOTQxOTAsIm5iZiI6MTUxNzM5MDU5MCwianRpIjoiWUxtdXJ5TkV2UzlwSEExTyJ9.nWV7OWLYdMJmZCHNP9tFuAmZ84DwzYO00O3jQ_RfhXQ"

So, how to extract that string from the object in my Ionic app.

1
  • i think you have to do result['token'] Commented Jan 31, 2018 at 9:39

3 Answers 3

2

What You're trying to do is storing the whole JSON i.e result returned from the server in variable 'tokenn'.

Instead try mapping token from result to store just the string part.

//storing the retured Token into Ionic Storage.
this.storage.set("tokenn", result['token']);
console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

1

It’s simple:

let loginToken = JSON.parse(result).token; console.log(loginToken); //Will print exactly what you need And then you can store the login token in the storage.

Basically with JSON.parse you’re converting a JSON in to an object

Comments

1

Because you are storing the whole object in storage. you can solve this in two ways 1. you store only string to storage and access it.

this.storage.set("tokenn", result.token);
const result = this.storage.get("tokenn");
console.log(result);

  1. store the whole JSON and access only string

this.storage.set("tokenn", result);
const result = this.storage.get("tokenn");
console.log(result.token);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.