JSON data is returned as Object Object

Trying to work with a JSON object and need to stringify it for localStorage:

$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');
});

Retrieving the data from localStorage:

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

When logging the profile, it shows [Object object]. Need help converting it back into an object.

EDIT: JSON Data:

[
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunasdfgk.com',
token: '',
platform: '',
password: 'inc3',
companyProfileID: 1,
authentication: '',
UserTopics: [...]
}

The current log of profile displays a complex structure. Help needed in parsing the JSON correctly.

Answer №1

For debugging purposes, try using console.log(profile). Your current approach is converting the "profile" variable to a String, hence resulting in [Object Object]. In order to access the email property, you should use profile[0].email since your function returns an array instead of a JSON object.

Answer №2

you are on the right track

console.log( profile); will display the accurate object

Answer №3

It appears that there may be a Cross-origin-policy issue at play here. Check out the following article for more information:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Enhancement in Chrome: Inclusion of Origin header in same-origin requests

When we POST an AJAX request to a server running locally, the code looks like this: xhr.open("POST", "http://localhost:9000/context/request"); xhr.addHeader(someCustomHeaders); xhr.send(someData); The webpage where this javascript is executed is also on ...

Error with the jQuery scrolling plugin

Currently, the script is set up this way: jQuery(document).ready(function(){ var windheight = jQuery(window).height(); var windTop = jQuery(window).scrollTop(); var windbottom = windheight + windTop ; jQuery.fn.revealOnScroll = function(){ return this.e ...

Vuejs tutorial: Toggle a collapsible menu based on the active tab status

I am using two methods called forceOpenSettings() and forceCloseSettings() to control the opening and closing of a collapsible section. These methods function properly when tested individually. However, I need them to be triggered based on a specific condi ...

Utilize Javascript to convert centimeters into inches

Can anyone help me with a JavaScript function that accurately converts CM to IN? I've been using the code below: function toFeet(n) { var realFeet = ((n*0.393700) / 12); var feet = Math.floor(realFeet); var inches = Math.round(10*((realFeet ...

Issue with AngularJS filter not functioning properly

I have a specific situation where I am using an ng-repeat directive in the following manner: {"ng-repeat" => "city in cities() | filter: search"} In this context, each city object is structured like so: { attributes: {name: 'Boston'} } Furt ...

Developing a music playlist using javascript/angular (replicating array elements while linking nested objects)

I am currently working on creating a playlist for a music player within an Angular app that includes a shuffle feature. The actual shuffle function is not the issue, as I am utilizing the Fisher Yates Shuffle method and it is functioning correctly. When th ...

Waiting for update completion in Firebase Firestore (Javascript for web development)

When I retrieve a document, update it, and then try to access the updated data, I am getting undefined logged. Can anyone explain why this is happening and suggest a solution for successfully fetching the new data from the document? db.collection("collect ...

Trouble with AngularJS ui-router: template fails to show up

I have been diving into a tutorial on egghead.io that delves into application architecture, tweaking certain components to fit my specific application needs. Just a heads up, I am relatively new to Angular, so I'm hoping the issue at hand is easy to ...

Ways to extract data from JSON web services

Below is the JSON response retrieved from a web-service. {"message":"success","data":[{"push_status":"1"}]} The task is to extract the value of push_status from {"push_status":"1"}. This snippet of code is used for this purpose: public static VehicleDe ...

Having trouble updating an array in a mongoose document?

Need help with updating an array in a document by adding or replacing objects based on certain conditions? It seems like only the $set parameter is working for you. Here's a look at my mongoose schema: var cartSchema = mongoose.Schema({ mail: Stri ...

Changing the fill color of an SVG pattern remains unchanged

I have been working with Vue.js to create SVGs with shape patterns as background. The patterns themselves are functioning correctly, but I am encountering an issue when attempting to dynamically change the color of the pattern filling by passing props. D ...

I am interested in extracting information from the Firebase real-time database and showcasing it on my HTML webpage

I am struggling to display data from the Firebase real-time database on my HTML page. Even though I can see all the data perfectly in the console, it doesn't show up on the webpage. I attempted to use a for loop, but it still doesn't display the ...

Hiding a div with Javascript when the Excel dialog box is loaded

I have a piece of JavaScript code that is activated when the user clicks on an excel image. $("#excel").on("click", function () { $('#revealSpinningWheel').reveal(); $(window).load(function () { $('#revealSpinningWheel').hide ...

Assign the filename to the corresponding label upon file upload

I've customized my file uploader input field by hiding it and using a styled label as the clickable element for uploading files. You can check out the implementation on this jsFiddle. To update the label text with the actual filename once a file is s ...

Transforming numerous key-value pairs into JSON formatting: utilizing sed in conjunction with an awk for loop

I am currently working with a data file that has 8 columns delimited by pipes. The last column contains an unpredictable number of key-value pairs, each separated by the equals sign and spaces between each pair. However, the challenge is that the values wi ...

The 'Subscription' type does not contain the properties _isScalar, source, operator, lift, and several others that are found in the 'Observable<any>' type

Looking to retrieve data from two APIs in Angular 8. I have created a resolver like this: export class AccessLevelResolve implements Resolve<any>{ constructor(private accessLevel: AccessLevelService) { } resolve(route: ActivatedRouteSnapshot, sta ...

What is the correct location for the webpack.config.js file within a React project?

I recently inherited a React project that I need to continue working on, but I have never used React before. I've been advised to use a web worker in the project and have found a tool that seems suitable for my needs: Worker Loader The suggested meth ...

Animating a dotted border path in SVG for a progress bar effect

I am attempting to create an animation for a dotted SVG circle that resembles a progress bar, where it fills itself over a duration of 3 seconds. However, I am facing difficulties in achieving this effect with the dotted border. The current code I have doe ...

Discover the power of utilizing the reduce function to extract the value of a specific field within an array of Objects

In the following example, we have an object with 3 forms: formA, formB, and formC. Form A and B are objects, while formC is an array of objects that can contain multiple items. const object: { "formA": { "details": {}, ...

Confirm the checkboxes that are necessary

In the realm of my AngularJS project, I am eager to develop a terms and conditions form. Among multiple checkboxes, only select few are deemed necessary. My aim is to activate the submit button solely when all required checkboxes have been checked. Below ...