Skip to content
Back

How to avoid Escape char backslash on string[] response - Angular 17

  • 0
  • Databases
  • Web
ICE
12 Mar, 2024, 20:37

Service.ts getOrder() { async getOrders():Promise<any> { return await this.database.listDocuments(apiConfig.DB_ID, apiConfig.ORDERS_ID); } }

Component.ts this.backendService.getOrders().then((res) => { this.orders=res.documents; }

I am using *ngFor to display database reply

TL;DR
Developers are having trouble with JSON parsing in an Angular application, leading to empty values and errors in the UI. The issue seems to be related to the parsing of the order items. One suggested solution involves using JSON.parse within a map function. However, attempts to fix the problem have been unsuccessful so far.
Kenny
12 Mar, 2024, 20:39

Are you stringifying the order items before insert into the database?

Kenny
12 Mar, 2024, 20:39

If so you'll need to json parse them before you can use the items in that object.

ICE
12 Mar, 2024, 20:41

yes, i am doing strinfify when inserting the data

ICE
12 Mar, 2024, 20:41

for (let i=0; i <items1.length; i++) { console.log("for loop", items1[i]); let value:string = JSON.stringify(items1[i]); itemsofstring.push(value); }

Kenny
12 Mar, 2024, 20:42

I don't know how efficient this is, but you can do something like

let orderItems = OrderItems.map(x => JSON.parse(x));

ICE
12 Mar, 2024, 20:42

parse is not working because of the escape char

Kenny
12 Mar, 2024, 20:49
TypeScript
orders = ["{\"test\":\"test\"}"];
newOrders: any = [];

constructor() {
  this.newOrders = this.orders.map(order => JSON.parse(order));
}
TypeScript
<p *ngFor="let item of newOrders">{{ item.test }}</p>

This worked just fine for me

Kenny
12 Mar, 2024, 20:50

But for your model, you'll probably want to extent Models.Document from appwrite. So you have access to the default attributes like $database, $createdAt, etc.

Kenny
12 Mar, 2024, 21:11

Do neither of these work?

ICE
12 Mar, 2024, 21:48

they didnt work

Kenny
13 Mar, 2024, 15:07

You wouldn't wrap it in curly braces, if you do wrap it you have to say you're returning JSON.parse(el2)

Kenny
13 Mar, 2024, 15:08

Maybe try something like this?

TypeScript
this.backendService.getOrders().then((res) => {

this.itemofOrder =  res.documents.map((el:any) => { 
       return el.orderItems.map((el2:any) => JSON.parse(el2));
   });
ICE
13 Mar, 2024, 21:54

Nope it didnt work.

ICE
13 Mar, 2024, 21:55

This is my orderItems as sub object of response.

[{"productName":"Moto Edge 40 Neo","productPrice":799,"productDetails":"Moto Edge 40 Neo 256GB","productAvailable":true,"qty":1,"$id":"65f21c437b6056836618","$createdAt":"2024-03-13T21:36:03.506+00:00","$updatedAt":"2024-03-13T21:36:03.506+00:00","$permissions":[],"$databaseId":"smartCartDB","$collectionId":"Products"},{"productName":"Samsung s25","productPrice":1499,"productDetails":"Samsung mobile, 1TB","productAvailable":true,"qty":1,"$id":"65f21c2d5ded54bcacf3","$createdAt":"2024-03-13T21:35:41.385+00:00","$updatedAt":"2024-03-13T21:35:41.385+00:00","$permissions":[],"$databaseId":"smartCartDB","$collectionId":"Products"}]

ICE
13 Mar, 2024, 21:56

component.html <p *ngFor="let item3 of order['orderItems'] "> Qty:{{ item3.qty }}, Price: {{item3.productPrice}},Name: {{item3.productDetails }}</p>

in the UI, -- not getting the values Qty:, Price: ,Name:

Qty:, Price: ,Name:

ICE
13 Mar, 2024, 21:57

why the *ngFor is not showing the values. but it loops twice.

Kenny
13 Mar, 2024, 21:59

It's probably because you're not properly parsing the json in order items. So qty, price, details do not exist. What do you see if you do something like {{ item3 | json }} or even just {{ item3 }}

ICE
13 Mar, 2024, 22:03

Getting empty values

ICE
13 Mar, 2024, 22:03

but orderItems displays value.

ICE
13 Mar, 2024, 22:03

when i do {{item3 | json }}, getting empty

Kenny
14 Mar, 2024, 01:28

What about if you just do item3?

Slava
14 Mar, 2024, 09:13

what does {{order['orderItems'] | json}} shows in your UI?

ICE
17 Mar, 2024, 13:59

I am getting empty

ICE
17 Mar, 2024, 14:00

I dont know why JSON.parse not working and complaining unexpected white space

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more