user3407039 user3407039 - 1 year ago 83
Javascript Question

AngularJS: Storing an object in a cookie giving result of [Object Object]

I am trying to store a group of user credentials in a cookie when sending in this user object to my service -

this.SetCookie = function (user) {
$cookies.user = user;

However what I get when I try to retrieve this cookie I do not get an object but just a string that says "[Object Object]"

I can store all the user credentials individually in their own cookies, as I know I can make that work, but it seems pretty inefficient? Is there an easy fix for this? The top result I found for this problem was related to JQuery and did not work for me.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download