The term 'Employee' is typically used as a classification, but in this context, it is being treated as a specific value

I am encountering an issue while trying to define a variable as type Employee. The error message 'Employee' only refers to a type but is being used as a value here. ts(2693) keeps appearing.

Here is my show-api.ts code:

import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';

interface Employee {
    Id: String;
    EmployeeName: String;
    StartTimeUtc: String;
    EndTimeUtc: String;
    EntryNotes: String;
    DeletedOn: String;
}

@Component({
selector: 'app-show-api',
templateUrl: './show-api.component.html',
styleUrls: ['./show-api.component.css']
})

export class ShowApiComponent implements OnInit {

li:any;
lis: any=[];

constructor(private http : HttpClient){
    
}

ngOnInit(): void {
    this.http.get('API URL of my JSON file')
    .subscribe(Response => {
    
    if(Response){
        hideLoader();
    }

    **employee: Employee[]=Response**;

    
    });
    function hideLoader(){
    document.getElementById('loading')!.style.display = 'none';}
}}

Data retrieved from the API:

[

{"Id": "aa",
"EmployeeName": "bb",
"StartTimeUtc": "cc",
"EndTimeUtc": "dd",
"EntryNotes": "ee",
"DeletedOn": null },

{"Id": "ff",
"EmployeeName": "gg",
"StartTimeUtc": "hh",
"EndTimeUtc": "ii",
"EntryNotes": "jj",
"DeletedOn": null },

]

If the above issue cannot be resolved, kindly suggest an alternative method to fetch data from a JSON file.

Answer №1

The issue here lies in the improper declaration of the variable and not correctly mapping Response to the interface provided.

ngOnInit(): void {
    this.http.get('URL for my JSON data')
    .subscribe(Response => {

    
    if(Response){
        hideLoader();
    }

    //oldCode -> **employee: Employee[]=Response**;
    /* New code */ const employee: Employee[] = Response as Employee[];
    
    });
    function hideLoader(){
    document.getElementById('loading')!.style.display = 'none';}
}}

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

A mistake was made: The template contains errors stating that 'app-my-profile' is an unknown element

I encountered an error message: "Uncaught Error: Template parse errors: 'app-my-profile' is not a known element," while working on setting up my profile service. import { BrowserModule } from '@angular/platform-browser'; import { NgM ...

Tips for integrating material icons into your project

Having some trouble using Outlined material icons in my application. Struggling with the import process and can only access the normal filled icons instead of the outlined ones. I would like to incorporate outlined icons similar to those shown in this exam ...

Tips for Mastering Animation Anchoring with Angular 2

Did you know that AngularJS 1.4 has a unique animation feature called Animation Anchoring? This feature allows you to mark elements in both the source and destination pages with the attribute ng-animate-ref, creating a computed animation between the two b ...

Restricting a Blob to a particular data type

As seen in the GitHub link, the definition of Blob is specified: /** A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functional ...

Viewability of external values in angular designs

Currently, I am facing an issue where multiple modules have duplicated options within the class field: ... options = ['opt1','opt1'] ... To solve this problem, I want to move the duplicated options to a constants module and then im ...

The ExpressJS Req.method TypeError occurs when attempting to read the 'method' property of an undefined object

My node express server is throwing an error: Error in index.js. const bodyParser = require('body-parser'), express = require('express'), path = require('path'); const config = require('./config'); con ...

Unable to start Angular application, encountering errors while running ng serve

The challenge at hand As I delve into a new project, I've successfully cloned the repository onto my local machine. After utilizing npm install to fetch the necessary packages, running ng serve triggers a series of errors. Despite the application fai ...

Improving the method of retrieving RTK result within getServerSideProps

Currently, I am utilizing RTK Query to make an API call within my getServerSideProps function. While I can successfully retrieve the result using the code snippet provided below, I find the process somewhat awkward. Additionally, the result lacks proper ty ...

Having trouble with Angular2's Maximum Call Stack Exceeded error when trying to display images?

I am facing an issue in my Angular2 application where I am trying to display an image in Uint8Array format. The problem arises when the image size exceeds ~300Kb, resulting in a 'Maximum Call Stack Exceeded' error. Currently, I have been able to ...

Uncovering TypeScript's Type Inference Power Through the keyof Keyword

Recently, I encountered a situation where I needed to utilize an abstract class. export abstract class ABaseModel { public static isKeyOf<T>(propName: (keyof T)): string { return propName; } } Following that, I also created another class wh ...

Manipulating a DOM element in Angular 2 to alter its class attribute

I am a beginner in angular2. Here is my code: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'main', template: ` <div class="current"> </div> ` }) export class MainComponent impl ...

Identifying the Click Event Within an ngx Bootstrap Modal

I recently set up an ngx bootstrap modal using the instructions provided in this helpful guide - . However, I'm facing a challenge in detecting click events within the modal body once it's open. Below is the code snippet from my app component. D ...

When using a typescript subscription to collect data from an API, the information is stored in an array. However, only one piece of data can be

I have implemented a method to fetch data from an API using Angular: ngAfterViewInit() { this.exampleDatabase = new ExampleHttpDatabase(this._httpClient); var href = '/schuhe-store/status'; if (environment.production === false) { href ...

I am trying to figure out how to retrieve the name of the property that is bound to [(ngModel)] in Angular 6

Here is a custom component example: <form-text [(ngModel)]="dataItem.prop1"> </form-text> Is there a way to extract the property name "prop1" from the class in this scenario? @Component({ selector: 'form-text', template ...

Workspace watch mode does not update Typescript definitions

Greetings to all who are reading this, I have created a React micro-frontend project using SPA v5.9, Webpack v5.75, Babel-loader v9.1, Ts-loader v9.4, and Yarn workspace v3.5. The project structure is very basic: Root SPA => Package Root Application S ...

What is the process for server-side rendering an Angular application with Angular Universal and incorporating an SSL certificate?

Typically, when I want my angular applications to run locally over HTTPS, I install a certificate and make changes in the angular.json file like this: "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { " ...

Downloading a CSV file using Angular 8 and Django RestFramework

I am a beginner in Angular and I'm looking to implement a feature where users can download a CSV file upon clicking a download button. Django viewset @action(detail=False, methods=['get']) def download_csv(self, request): data = { ...

Using RouterModule.forRoot() in Angular 2 CLI compiler/ngc command: A step-by-step guide

Assume I have the appRoutingModule shown below: export const routes: Route[] = [ { path: '', component: ApplicationlistComponent } ]; @NgModule( { imports: [ ApplicationsModule, RouterModule.forRoot( routes ) ], exports: [ Route ...

Guide to creating a one-to-one object literal map with a different value type using a function return without explicitly defining the return type

At the moment, I have successfully managed to combine the keys and values of each object literal that is passed into a function. For example: interface StaticClass<T = any> { new (...args: any[]): T } type RecordOfStaticClasses = Record<string, ...

Angular 5 - Performing Jasmine Testing: Simulating an Error Response on a Genuine HTTP Request

Before I start, I'd like to mention that I am currently in the process of learning Angular 4 as part of an internship. This is all very new to me. Anyway, I have a requirement where I need to simulate an error during an HTTP request in the controller ...