AngularJS: Storing an object in a cookie giving result of [Object Object]
In Angular 1.4 I found that storing a JSON object by creating the cookie like this:
var obj = { currentUser: { username: "testUN", authdata: authdata } }; $cookies.putObject('cookieName', obj);
Allows you to get the cookie back like this:
var cookieWObject = $cookies.getObject('cookieName');
Then get to the values like this:
var username = cookieWObject.currentUser.username;var authdata = cookieWObject.currentUser.authdata;
You are printing object directly that will always show [Object Object]
because it contains an object of JSON
, while printing it alert method it uses .toString()
method to show it in console.
You need to make that JSON.stringify(user)
that will convert all the JSON
object to string. While using that object you need to use JSON.parse(user)
that will convert object to JSON
JSON.stringify(user); //for storing in cookiesJSON.parse($cookies.user); //for converting into an object