Issue with Angular 6: Textarea displaying value as Object Object

I have data saved in local storage using JSON.stringify, and I want to display it in a textarea.

Here are the relevant code snippets:

{
  "name": "some name"
}

To retrieve the data, I'm using this:

this.mydata = localStorage.getItem('mydata');

The data is stored in localStorage under the key 'mydata'.

When I log this.mydata to the console, it displays as follows:

{
  "name": "some name"
}

Now, to display this data in a textarea, I'm using the following code:

this.con.nativeElement.value = JSON.parse(this.mydata);

However, when displayed in the textarea, it shows as:

[object Object]

What I actually want to see is:

{
    name: 'some name'
}

In my textarea, I've added the json pipe like this:

{{ thecontents | json }}

So theoretically, it should display the object properly.

How can I resolve this issue?

Answer №1

The reason for this issue is that you are trying to assign a JSON object to a text field instead of displaying the string value.

To resolve this, use the Json.stringify() method on your data and then pass the resulting string to your text field.

Answer №2

To properly handle the data,

const data = JSON.parse(localStorage.getItem('data'));

After parsing,

{{ data | json }}

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

I am experiencing some difficulty with the GetServerDate using the JSON protocol in JQuery; it's

I am facing an issue while trying to execute a basic ajax call to fetch data from a file on a server. Even though I can access the file via the web browser and have diligently followed tutorials related to this topic, I have hit a dead end. Here is the sn ...

"Embrace the powerful combination of WinJS, Angular, and TypeScript for

Currently, I am attempting to integrate winjs with Angular and TypeScript. The Angular-Winjs wrapper functions well, except when additional JavaScript is required for the Dom-Elements. In my scenario, I am trying to implement the split-view item. Although ...

Template not being properly populated by handlebar for JSON output

Hello there! I am currently working on making an http GET call to an MVC controller action which returns JSON data. The JSON data looks something like this: [ { "PlanCode": "P001", "PlanName": "Plan1" }, { "PlanCode": " ...

What is the process for sending a JSONArray message to a device using GCM from a PHP server?

I'm currently working on sending notifications to a device. I've successfully sent regular string messages, but now I'm curious if it's possible to send a JSONArray message instead. If so, could you provide guidance on how to parse this ...

Converting JSON data to a pandas DataFrame requires the list indices to be integers

I have a large JSON dataset that I want to convert to CSV for analysis purposes. However, when using json_normalize to build the table, I encounter the following error: Traceback (most recent call last): File "/Users/Home/Downloads/JSONtoCSV/easybill.py" ...

Create a dynamic styled component with tags based on props

Looking to craft a dynamic tag using styled components, where the tag is passed in via props. Here's an example of the code: import * as React from 'react'; import styled from 'styled-components'; type ContainerProps = { chi ...

Mapping JSON data from Mongoose to Vue and Quasar: A comprehensive guide

I have set up a Mongoose backend and created some REST APIs to serve data to my Vue/Quasar frontend. The setup is pretty basic at the moment, utilizing Node/Express http for API calls without Axios or similar tools yet. I have successfully implemented simp ...

Expressing a dictionary through parameters in a print statement

Exploring the possibilities of adjusting XHR request parameters to modify the dictionary object contents retrieved. The initial code snippet to begin this process is: teamStatDicts = responser[u'teamTableStats'] for statDict in teamStatDicts ...

When using Inertia.js with Typescript, an issue arises where the argument types {} and InertiaFormProps{} are not compatible with the parameter type Partial<VisitOptions> or undefined

I set up a brand new Laravel project and integrated Laravel Breeze along with Typescript support. After creating a form (using useForm()) and utilizing the .post() method with one of the options selected (such as onFinish: () =>), I encountered the fol ...

Issue with FILE_APPEND and file_put_contents function not functioning as expected

My approach involves sending form data as JSON via AJAX to a PHP file, which then saves the JSON information in a text file on the server. An issue arises when using FILE_APPEND, as the data is not written to the file if the JSON content already exists wi ...

Using TypeScript to deserialize various types from a shared object

I am currently dealing with a JSON array containing serialized objects, each of which has a type field. My challenge lies in deserializing them properly due to TypeScript not cooperating as expected: Check out the TypeScript playground for reference. type ...

How to Use Hyperledger Composer's Rest-Server-Api Local-Passport Strategy in an Angular Application without Node.js

Is it possible for me to implement the passport-local strategy in my project, considering that I am using an angular front-end generated by the composer-rest-server tool? I noticed in the documentation for passportjs regarding passport-local, it mentions ...

Exploring the capabilities of JQuery UI tabs' beforeLoad feature using JSON data

Trying to dynamically load content of a tab using an ASP.NET method decorated with the [WebMethod] attribute. [WebMethod] public static string Result() { return RenderControl("WebUserControl1.ascx"); } It functions correctly when the tab is loaded wit ...

The Formik and React error is indicating that the '{ refetch: any; }' type is absent

When attempting to pass a prop down to my EmailSignupScreen, I encountered an error message. This issue arose while experimenting with Formik and Typescript. "message": "Type '{ refetch: any; }' is missing the following properties from type &apo ...

Python script for merging JSON files

I am facing an issue while attempting to append multiple json files in Python. Despite having what seems like the correct code, I encounter an error. Here is the code snippet: import pandas as pd df1=pd.DataFrame() for i in range(0,49): df = pd.read ...

Converting HashMap to JSON representation

Take a look at this HashMap that I'm attempting to parse: HashMap map = new HashMap(); map.put("bowser", "b=mozilla"); map.put("car", "car=Ford"); map.put("model","model=Mustang"); map.put("Year", 2014); map.put("dealer", "Dealer=AKHI"); I initi ...

Navigating nested data structures in reactive forms

When performing a POST request, we often create something similar to: const userData = this.userForm.value; Imagine you have the following template: <input type="text" id="userName" formControlName="userName"> <input type="email" id="userEmail" ...

Exploring the use of national flag emojis in VS code

I've been attempting to use flag emojis as reactions on a Discord bot, but they just won't cooperate. Every time I try something like this > ':flag_jp:', I encounter the following error: Error: DiscordAPIError: Unknown Emoji EDIT ...

When implementing ReplaySubject in Angular for a PUT request, the issue of data loss arises

I seem to be encountering a problem with the ReplaySubject. I can't quite pinpoint what I've done wrong, but the issue is that whenever I make a change and save it in the backend, the ReplaySubject fetches new data but fails to display it on the ...

There appears to be an issue with Javascript's ability to handle JSON API requests

I'm currently working on a webpage that utilizes the openweathermap API to showcase the user's city and local temperature. Unfortunately, I'm encountering an issue where the JSON API is not being processed and nothing is happening. Despite r ...