Issue encountered when attempting to access disk JSON data: 404 error code detected

I am attempting to retrieve 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 displayed below:

https://i.sstatic.net/4U8XR.png

An error message is shown as follows:

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

Despite having the correct filepath in the source folder, why am I encountering a 404 error?

Thank you.

Answer №1

Do not include src in the URL.

Once the project is compiled, the assets folder will appear in the root directory.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There was an issue retrieving the token in the interceptor. The type 'Promise<void | Observable<HttpEvent<any>>>' does not match the expected type 'Observable<HttpEvent<any>>'

I am currently working on developing an interceptor that sends the token along with requests for the Api. In order to store user information, I am utilizing the @ionic/storage library. However, I am facing an issue where my interceptor cannot access the t ...

Issue with accessing storage in Ionic Storage (Angular)

Currently, I am attempting to utilize Ionic storage for the purpose of saving and loading an authentication token that is necessary for accessing the backend API in my application. However, I am encountering difficulties retrieving the value from storage. ...

Guide on linking enum values with types in TypeScript

My enum type is structured as follows: export enum API_TYPE { INDEX = "index_api", CREATE = "create_api", SHOW = "show_api", UPDATE = "update_api", DELETE = "destroy_api" }; Presently, I have a f ...

Having trouble locating the Nativescript-theme-core file for your Nativescript application?

I'm currently working on a basic barcode scanner app and encountering an unusual error once the app is deployed to a Genymotion emulator. It appears to be searching for the core theme in the incorrect location. Any thoughts on why this issue is occurr ...

The computed function is unable to find the property on the specified type

I am a beginner in TypeScript. I have encountered an issue with the code below, which runs perfectly fine in JavaScript but is not compiling here. export default { data: function() { return { data: [ 'Angular', 'A ...

The (functionName) does not exist within the subclass as a valid function

I am currently developing an extension for a web-based text editor. However, I am facing some unexpected results due to the class hierarchy in my code. Despite attempting to relocate the "validate" function to the base class, I have not been successful in ...

I'm having trouble with my code not working for get, set, and post requests. What could be causing this issue and how can I

Here are the TypeScript codes I've written to retrieve product details and delete them. import { Component, OnInit } from '@angular/core'; import {FormGroup,FormBuilder, FormControl, Validators} from "@angular/forms" // other impor ...

Issue with setting active slide value in ng2 Bootstrap Carousel – constantly defaults to slide 1

My current project implementation involves using . Although I have successfully implemented it, I am encountering an issue where slide 1 remains active at all times. I am unsure how to remove the active class from slide 1. The main challenge lies in sett ...

Angular CLI Issue: Project Configuration Not Detected

I've been attempting to execute an Angular program that was developed by a coworker, but I keep encountering the same error message: "The serve command needs to be run in an Angular project, yet a project definition cannot be found." Even after runnin ...

Error in Angular 5: Cannot find property 'then' in type 'Observable<any>'

I encountered the following error message: "[ts] Property 'then' does not exist on type 'Observable'. How can I resolve this issue? Below is my Component code: getUsers(){ this.authService.getUsers().then((res) => { thi ...

What are the advantages of combining the website URL and API URL within the Angular service?

When deploying my application in a production environment, I encounter an issue with the URL addresses. The web address is , while the API address is . However, when making a request to the API through Angular, the URLs get concatenated into . This issue d ...

Are there performance concerns associated with invoking functions in templates within Angular 2+?

I'm currently adjusting to Angular's change detection mechanism, and I'm uncertain if invoking functions in templates can impact performance. For instance, which is better: <mat-tab-group> <mat-tab label="First"> {{ getFirstT ...

Angular routerlink params can be secured using encryption and decryption techniques

Can anyone provide me with a reliable method to encrypt and decrypt querystring parameters, such as gmail.com? Also, I am looking for information on how to obtain these query parameters. The path structure I am working with is: { path: 'myPrograms/: ...

What steps can I take to ensure my dynamic route functions correctly in NextJs?

// /home/[storeId]/layout.tsx import prismadb from "@/lib/prismadb"; import { auth } from "@clerk/nextjs/server"; import { redirect } from "next/navigation"; export default async function DashboardLayout({ children, params, ...

Could you explain the distinction between npm install and sudo npm install?

I recently switched to using linux. To install typescript, I ran the following command: npm i typescript Although there were no errors during the installation process, when I checked the version by typing tsc --version, I encountered the error message -bas ...

How can I use Angular to dynamically open a video that corresponds to a clicked image?

I need assistance with a functionality where clicking on an image opens a corresponding video on the next page. The issue is that all images have the same ID, making it difficult to link each image to its respective video. Below is the data I am working ...

Developing middleware for managing event handlers

Scenario: I am tasked with managing multiple events that necessitate an "available client". Therefore, in each event handler, my first step is to attempt to acquire an available client. If no client is available, I will send a "Service unavailable" messag ...

What is the best way to send an XML body using Angular 5 with POST method?

I am currently developing an Ionic application that needs to make REST API calls with XML as the body. However, I am facing issues in getting the call to post with XML. This is my LoginProvider where I utilize DOMParser to parse data to XML before sending ...

Display responsive input field based on selected option in ionic2 dropdown menu

When the user selects 'Other' from the dropdown menu using Ionic2 and Angular2, I want to provide them with an option to enter their profession. Here is a visual representation of the select box: Below is the code snippet for achieving this fun ...

Leveraging AngularJS services within an Angular service

I am currently in the process of transitioning my AngularJS application to Angular. To facilitate this transition, I plan to create a hybrid application that combines both frameworks until the conversion is complete. However, I have encountered an issue wi ...