Austin Hunter Austin Hunter - 2 months ago 18
Javascript Question

JSON returns Object Object

I currently have a JSON object I am trying to use. Here is me stringifying it because localStorage can only have strings:



$http.post('http://localhost:8000/refresh', {
name: $scope.name,
email: $scope.email,
token: $rootScope.devToken,
platform: ionic.Platform.platform()
}).then(function(response) {
console.log("Saving profile");

window.localStorage.setItem("UserProfile", JSON.stringify(response.data));

$state.go('home');
});





When I
console.log(response.data)
, the correct data comes out.
Then here is me getting it out of localStorage:



var temp = window.localStorage.getItem("UserProfile");
var profile = JSON.parse(temp);
console.log("Profile: " + profile);





When I
console.log(profile)
I get Object object. What am I doing wrong? When I
console.log(temp)
I get a huge string of correct data. But I don't want it to be a string. I need it to be back into an object.

EDIT:
JSON:



[
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunasdfgk.com',
token: '',
platform: '',
password: 'inc3',
companyProfileID: 1,
authentication: '',
UserTopics: [
{
topicID: 1,
topicName: 'Needs Maintenance',
alertLevel: 'Urgent',
TopicDepartments: [
{
departmentID: 1,
departmentName: 'Shop',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunook.com',
token: '',
platform: '',
companyProfileID: 1
}, {
userProfileID: 2,
firstName: 'Ashley',
lastName: 'Jeanette',
email: 'ashlhgfdail.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}
]
}, {
topicID: 2,
topicName: 'Help',
alertLevel: 'Urgent',
TopicDepartments: [
{
departmentID: 1,
departmentName: 'Shop',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'afafaf@oook.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}, {
departmentID: 2,
departmentName: 'Office',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Ashley',
lastName: 'Jeanette',
email: 'ashfafaff.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}
]
}
]
}
];





And console.log(profile) gives me this:



[Log] Array (1) (controllers.js, line 65)
0 Object

UserTopics: [Object, Object] (2)

_id: "57e078cc62e223290851c2c1"

authentication: ""

companyProfileID: 1

email: "ahun______.com"

firstName: "Austin"

lastName: "Hunter"

password: "inco_______23"

platform: ""

token: ""

userProfileID: 1

Object Prototype

__defineGetter__(propertyName, getterFunction)

__defineSetter__(propertyName, setterFunction)

__lookupGetter__(propertyName)

__lookupSetter__(propertyName)

constructor: function()

hasOwnProperty(propertyName)

isPrototypeOf(property)

propertyIsEnumerable(propertyName)

toLocaleString()

toString()

valueOf()

Array Prototype
No Properties.

Object Prototype

__defineGetter__(propertyName, getterFunction)

__defineSetter__(propertyName, setterFunction)

__lookupGetter__(propertyName)

__lookupSetter__(propertyName)

constructor: function()

hasOwnProperty(propertyName)

isPrototypeOf(property)

propertyIsEnumerable(propertyName)

toLocaleString()

toString()

valueOf()




Answer

try console.log(profile). your approach converts the "profile" to String. hence gives [Object Object] . in your case, to obtain email please use profile[0].email as your call return an array instead of a JSON Object.