Как отобразить FavRecipes от пользователя (ANGULAR) - PullRequest
0 голосов
/ 16 марта 2020

Я пытаюсь отобразить FavRecipes из текущего вошедшего в систему пользователя. Я пишу asp. net core MVC + Angular (SPA). У меня ошибка: 400 (неверный запрос) http://localhost: 5000 / api / users / undefined / favouriteRecipes Есть идеи?

Что я уже пробовал:

Resolver

import {Injectable} from '@angular/core';
import { Resolve, Router, ActivatedRouteSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AlertifyService } from '../_services/alertify.service';
import { UserService } from '../_services/user.service';
import { Recipe } from '../_models/recipe';


    @Injectable()
    export class FavRecipesResolver implements Resolve<Recipe[]> {

        constructor(private userService: UserService, private router: Router, private alertify: AlertifyService) {

        }

        resolve(route: ActivatedRouteSnapshot): Observable<Recipe[]> {
            return this.userService.getFavRecipes(route.params.id).pipe(
                catchError(error => {
                    this.alertify.error('Problem retrieving data');
                    this.router.navigate(['/home']);
                    return of(null);
                })
            );
        }
    }

Компонент

 import { Component, OnInit } from '@angular/core';
    import { User } from 'src/app/_models/user';
    import { AuthService } from 'src/app/_services/auth.service';
    import { UserService } from 'src/app/_services/user.service';
    import { AlertifyService } from 'src/app/_services/alertify.service';
    import { ActivatedRoute } from '@angular/router';
    import { Recipe } from 'src/app/_models/recipe';

    @Component({

      // tslint:disable-next-line: component-selector
      selector: 'app-favRecipe-lists',
      templateUrl: './favRecipe-lists.component.html',
      styleUrls: ['./favRecipe-lists.component.css']
    })
    export class FavRecipeListsComponent implements OnInit {
      recipes: Recipe[];

      constructor(private authService: AuthService, private userService: UserService,
                  private route: ActivatedRoute, private alertify: AlertifyService) { }

      ngOnInit() {
        this.route.data.subscribe(data => {
          this.recipes = data.recipes.result;
        });
      }

в моих маршрутах

{path: 'my-fav-recipes', component: FavRecipeListsComponent, resolve: {recipes: FavRecipesResolver}},

Модели:

export interface User {
    id: number;
    username: string;
    knownAs: string;
    age: number;
    gender: string;
    created: Date;
    lastActive: Date;
    photoUrl: string;
    city: string;
    country: string;
    introduction?: string;
    userPhotos?: UserPhoto[];
    recipes?: Recipe[];
}


export interface Recipe {
    id: number;
    name: string;
    ingredients: string;
    preparationTime: number;
    dateAdded: Date;
    photoUrl: string;
    description?: string;
    recipePhotos?: RecipePhoto[];
}

Должен ли я создать класс FavRecipes?

@ EDIT

Ответ API в Почтальоне enter image description here

В моем сервисе пользователя

getFavRecipes(id: number) {
    return this.http.get(this.baseUrl + 'users/' + id + '/favouriteRecipes');
  }
...