Я пытаюсь отобразить ответ от API и поместить его в html. Вот основные классы
app.component. html
<h1>sneaky calories work!!</h1>
your query is {{recipeResult.q}}
<div *ngFor = "let recipeIn of recipeResult.hits">
{{recipeIn.label}} {{recipeIn.url}}
<div>
app.component.ts
import { Component } from '@angular/core';
import { SearchRecipeService } from './recipe-service.service';
import { RecipeGlobal } from './pojos/recipe';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'SnearkyCalories';
recipeResult: RecipeGlobal;
constructor(private searchRecipeService: SearchRecipeService){}
ngOnInit(){
this.getRecipeInfo();
console.log(this.recipeResult.hits);
}
getRecipeInfo(){
this.searchRecipeService.getRecipeInfo().
subscribe(recipeResult => this.recipeResult = recipeResult);
}
}
Recipe.ts:
export interface RecipeGlobal {
q: string;
from: number;
to: number;
more: boolean;
count: number;
hits: Hit[];
}
export interface Hit {
recipe: Recipe;
bookmarked: boolean;
bought: boolean;
}
export interface Recipe {
uri: string;
label: string;
image: string;
source: string;
url: string;
shareAs: string;
yield: number;
dietLabels: string[];
healthLabels: string[];
cautions: any[];
ingredientLines: string[];
ingredients: Ingredient[];
calories: number;
totalWeight: number;
totalTime: number;
totalNutrients: { [key: string]: Total };
totalDaily: { [key: string]: Total };
digest: Digest[];
}
export interface Digest {
label: string;
tag: string;
schemaOrgTag: null | string;
total: number;
hasRDI: boolean;
daily: number;
unit: Unit;
sub?: Digest[];
}
export enum Unit {
Empty = "%",
G = "g",
Kcal = "kcal",
Mg = "mg",
Μg = "µg",
}
export interface Ingredient {
text: string;
weight: number;
}
export interface Total {
label: string;
quantity: number;
unit: Unit;
}
recipe-service.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Observable, of} from 'rxjs';
import {catchError, map, tap} from 'rxjs/operators'
import { Component, OnInit, Input } from '@angular/core';
import {RecipeGlobal, Recipe} from './pojos/recipe'
@Injectable({
providedIn: 'root'
})
export class SearchRecipeService {
constructor(
private http: HttpClient) {}
private searchUrl = 'https://api.edamam.com/search?q=bandeja paisa&app_id=xxxx&app_key=xxxxx&from=0&to=3&calories=591-722&health=alcohol-free';
getRecipeInfo(): Observable<RecipeGlobal>{
return this.http.get<RecipeGlobal>(this.searchUrl);
}
}
Я получаю эту ошибку в консоли chrome: не удается прочитать "совпадения" свойств с неопределенным
Я попытался распечатать объект прямо в консоли, но все равно выдает ту же ошибку. Похоже, что хиты не анализируются должным образом.