-1
I’m picking up an error while doing the Build of my angular application, the error is this:
Property 'resource_name' does not exist on type 'Produto[]'
Here is my TS code and HTML. The call by form
works normally. What I think is happening is that when I build the application the object Produto
is still vacant then has no property. What can I do in this case?
import { Component, OnInit } from '@angular/core';
import { Produto } from 'src/models/produto.models';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms'
import { ProdutoService } from '../services/produtos.service';
@Component({
selector: 'app-consultar-page',
templateUrl: './consultar-page.component.html',
styleUrls: ['./consultar-page.component.css']
})
export class ConsultarPageComponent{
produto = {} as Produto;
produtos: Produto[];
constructor(private produtoService: ProdutoService) {}
teste = true
ngOnInit() {
}
pesquisa(frm){
const data = this.produto.resource_id
this.produtoService.getProdutoByResourceID(data).subscribe((produtos: Produto[]) => {
this.produtos = produtos;
this.teste = false
})
}
<div class="uk-flex-center" uk-grid>
<div class="uk-card-body uk-card-default uk-background-muted uk-card-small ">
<div class="uk-margin"></div>
<h3 class="uk-card-title">CONSULTA DE RECURSOS</h3>
<form method="post" class="uk-form-horizontal uk-margin-large" #frm="ngForm" (ngSubmit)="pesquisa(frm)">
<div class="uk-margin">
<label class="uk-form-label" for="form-horizontal-text">Resource ID:</label>
<div class="uk-form-controls">
<input class="uk-input" type="text" placeholder="Resource ID" [(ngModel)]="produto.resource_id" name="resource_id" required>
</div>
</div>
<div uk-form-horizontal>
<button type="submit" class="uk-button uk-button-default uk-text-center">CONSULTAR</button>
</div>
</form>
</div>
</div>
<ng-template [ngIf]="!teste">
{{ produtos.resource_id }} || {{ produtos.resource_type}} || {{ produtos.resource_name }} || {{ produtos.owner_ }
</ng-template>
First that every class you use in another component should instantiate that class, usually in ngOnInit(), however, noting by the code posted I think the problem is that actually in the class Product there is no such property
produtos.resource_name
is sure that there exists this property, otherwise, if the object problem is empty it would give error in the previous properties like id and type.– LeAndrade
There is, and he complains about all other properties...
– Lucas Rodrigues
So the problem is in class instantiation.
– LeAndrade
I think you want to use an ngFor to iterate each element of the products
– Eduardo Vargas