1

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: '[email protected]',
									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()

4
  • 3
    Remove the string in the log - it's just how the log outputs. console.log(profile) - or console.log("profile", profile) Commented Sep 20, 2016 at 13:06
  • You're casting an object to a string while logging, the result is "[Object object]"… That's all that's wrong. Commented Sep 20, 2016 at 13:08
  • That works. Would accessing data still be the same? profile.email or profile.name? Commented Sep 20, 2016 at 13:13
  • For anyone with my same issue, Let me clarify. I had a array of objects with array of objects inside of it. The reason I couldn't just do profile.email was because it was an array. I have to do profile[0].email to get the email. Thank you all for the help! Commented Sep 20, 2016 at 13:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

That works. Would accessing data still be the same? profile.email or profile.name?
ofcourse it will work for profile.email / profile.name if you have them in your JSON.
add your JSON please.
JSON added. Just look above
For anyone with my same issue, Let me clarify. I had a array of objects with array of objects inside of it. The reason I couldn't just do profile.email was because it was an array. I have to do profile[0].email to get the email. Thank you all for the help!
|
1

your approach is correct

console.log( profile); will print correct object

Comments

-1

It might be a Cross-origin-policy issue.Have a look at this article.

https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document

1 Comment

No, it isn't. and this could better be a comment instead of an answer.

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.