Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda