0

I try to read JSON data from the disk using a service:

import { Product } from './../models/Product';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

type ProductRecord = Record<number, { product: Product; quantity: number }>;

@Injectable({
    providedIn: 'root'
})
export class ProductsService {
    productRecords: ProductRecord[] = [];
    currentPrduct: Product = {} as Product;

    constructor(private httpClient: HttpClient) {}

    public getProducts(): Observable<Product[]> {
        const url: string = 'src/assets/data.json';
        return this.httpClient.get<Product[]>(url);
    }

}

The folder structure is provided:

enter image description here

I receive an error provided below:

GET http://localhost:4200/src/assets/data.json 404 (Not Found)

As obviously the data is in the source folder and the location is correct, what is the reason that I receive a 404 error?

Thanks.

2
  • 1
    Remove src from the the url. Commented Aug 21, 2021 at 12:42
  • Thanks, this works for me after I remove the src Commented Aug 21, 2021 at 12:50

1 Answer 1

1

Remove src from the URL.

when the project becomes compiles, the assets folder comes in the root file.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.