Angular 6 HttpErrorResponse - PullRequest
       34

Angular 6 HttpErrorResponse

0 голосов
/ 23 октября 2018

В настоящее время я разрабатываю приложение Angular 6, работающее на сервере Node.js.Когда я пытаюсь вызвать http get http:localhost:3000/gallery, я получаю httpErrorResponse на стороне клиента, говорящий мне:

Ошибка Http во время синтаксического анализа для http://localhost:3000/gallery enter image description here

Это мой клиентский код в gallery.service.ts

import { GalleryItem } from "./gallery-item.model";
import { HttpClient } from "../../../node_modules/@angular/common/http";
import { Injectable } from "../../../node_modules/@angular/core";
import { Subject } from "../../../node_modules/rxjs";
import {environment} from '../../environments/environment';

const BACKEND_URL = environment.apiURL + "/gallery";

@Injectable({providedIn: "root"})
export class GalleryService {

    constructor(private http: HttpClient) {}
    private selectedItem: GalleryItem;
    private selectedItemUpdated = new Subject<GalleryItem>();
    private galleryItems: GalleryItem[] = [];
    private galleryItemsUpdated = new Subject<GalleryItem[]>();

    getGalleryItems() {
        console.log(1);
        this.http.get<{galleryItems: GalleryItem[]}>("http://localhost:3000/gallery").subscribe((getData) => {
            this.galleryItems = getData.galleryItems;
            this.galleryItemsUpdated.next([...this.galleryItems]);
        });
        console.log(2);
    }

    getGalleryItemsListener() {
        return this.galleryItemsUpdated.asObservable();
    }

    getItem(id) {
        this.http.get<{galleryItems:GalleryItem}>(BACKEND_URL + '/' + id).subscribe((getData) => {
            console.log(getData);
            this.selectedItem = getData.galleryItems;
            this.selectedItemUpdated.next(this.selectedItem);
        });
    }

    getItemUpdated() {
        return this.selectedItemUpdated.asObservable();
    }
}

В частности, это getGalleryItems(), который не работает.Любая помощь приветствуется!

...