0


Hello, There is a problem with a project that does not recognize a json file - and I do not know why. Is there anything I need to change or make it work?


this is my folders: enter image description here


this is my service:

import { Injectable } from "@angular/core";
import { Ibrides } from "./brides";
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';

@Injectable()
export class brideService {
private _brideUrl = 'api/brides.json';
constructor(private _http: HttpClient) { };

getBrides(): Observable<Ibrides[]> {

    return this._http.get<Ibrides[]>(this._brideUrl)
        .do(data => console.log('All:' + JSON.stringify(data)))
        .catch(this.handleError)
}
private handleError(err: HttpErrorResponse) {
    console.log(err.message);
    return Observable.throw(err.message);
}
}

this is my component


import { Component, OnInit } from '@angular/core';
import { Ibrides } from "./brides";
import { brideService } from "./brides.service"

@Component({
selector: 'pm-brides',
templateUrl: './brides_list.component.html',
styleUrls: []
})
export class bridesListComponent implements OnInit {


    constructor(private _brideService: brideService) {

    }
    errorMessage: string;
    brides: Ibrides[] = [];
    ngOnInit(): void {
    this._brideService.getBrides()
        .subscribe(brides => {
            this.brides = brides
        },

        error => this.errorMessage = <any>error);

    }

}
5
  • It's clearly because there is no API available for that URL. As I see a json file, don't you need a mock API call by any chance? Commented Sep 18, 2017 at 6:05
  • I have an internal json file, and I'm just trying to call him. Commented Sep 18, 2017 at 6:08
  • set the _brideUrl to 'app/api/brides.json', i.e. _brideUrl = 'app/api/brides.json'. Commented Sep 18, 2017 at 6:12
  • Try to set path with your src directory, will work. Commented Sep 18, 2017 at 6:13
  • the same eror :( Commented Sep 18, 2017 at 6:14

1 Answer 1

0

Just reference the file from the root level like this:

_brideUrl = 'app/api/brides.json'

For more information you can refer to this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.