Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Access Json Object return from express js with angular html template

I am trying to access JSON from angular html template. But it is not returning results. Simply showing Object or error by modification of code.

server.js

response.status(200).json(results.rows);

product.component.ts

export class ProductsComponent implements OnInit {
  products = {};
  usersJson: any[]=[];

  constructor(private service: Service) {

   }

  ngOnInit(): void {
    this.service.getResult().subscribe((data: JSON)=>{
      console.log(data);
      this.products = data;
      this.usersJson = Array.of(data);
      console.log(this.products);
    }) 
  }

data

data = [
 {product_id: 1, product_name:"Phone"},
 {product_id: 2, product_name:"laptop"}
]

product.component.html

<p>{{ products}}</p>
<h1>Product</h1>
<p>{{ products[0].product_id}}</p>
<p>{{usersJson[0]}}</p>
<p>{{usersJson[0].product_id}}</p>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use json pipe and '.?' notation to ensure that will not be problems with undefined fields:

<p>{{ products | json }}</p>
<h1> Product </h1>
<p>{{ products[0] != null ? ( products[0]?.product_id | json ) : 'N/A' }}</p>
<p>{{ usersJson[0] != null ? ( usersJson[0] | json ) : 'N/A' }}</p>
<p>{{ usersJson[0]?.product_id }}</p>

NOTE: If product_id is a number/string and not an object, change

( products[0]?.product_id | json ) 

to

( products[0]?.product_id )  [just delete the json pipe in that line]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!