Angular 12: TypeScript Issue TS2339 - Unable to Locate Property on Type

Whenever I use the code below, I encounter

error TS2339: Property 'timestamp' does not exist on type 'LogRepair[]'

In the component's HTML file, I am attempting to loop through an array of properties defined in the LogRepair type:

<div>
<div *ngFor = "let repair of repairs" class="repair_list">
    <div class = "RT">{{repairs.name}}</div>
    <div class = "Repair Type">{{repairs.type}}</div>
    <div class = "Gecko Number">{{repairs.phoneID}}</div>
    <div class = "Warranty Sticker">{{repairs.stickerID}}</div>
    <div class = "Description">{{repairs.description}}</div>
    <div class = "Timestamp">{{repairs.timestamp}}</div>
</div>

In the component's TypeScript file, I import the LogRepair model and declare a property called repairs:

import { Component, OnInit } from '@angular/core';
import { LogRepairService } from '../log-repair.service';
import { LogRepair } from '../LogRepair.model';


@Component({
  selector: 'app-view-repairs',
  templateUrl: './view-repairs.component.html',
  styleUrls: ['./view-repairs.component.scss']
})
export class ViewRepairsComponent implements OnInit {

  repairs: LogRepair[]=[];

  constructor(private logRepairService: LogRepairService){}
    // Called first time before the ngOnInit()
  ngOnInit(): void {
    // Called after the constructor and called after the first ngOnChanges() 

this.repairs = this.logRepairService.getRepairs();

  }
}

In the service file, I export a class LogRepairService which contains hard-coded data in the LogRepair format.

import { Injectable } from '@angular/core';
import { LogRepair } from './LogRepair.model';

@Injectable({
  providedIn: 'root'
})
export class LogRepairService {

  repairs: LogRepair [] =[
  {name:"Tom",type:"standard",phoneID:"12345",stickerID:"ZX57B",description:"",timestamp: new Date ('2020-02-25T23:18:21.932Z') },
  {name:"Ben",type:"Warranty",phoneID:"54321",stickerID:"B76Gf",description:"touch ic",timestamp: new Date ('2021-02-25T23:18:21.932Z') },
  ];

  constructor() { }

  getRepairs(){
    return this.repairs;
  }

  postRepair(repairs:LogRepair){

  }
}

Below is the model file with the timestamp property included:

export interface LogRepair
{
   name: string;
   type: string;
   phoneID: string;
   stickerID: string;
   description: string;
   timestamp: Date;
}

Since the timestamp property is present in the exported interface, I am facing challenges resolving this error.

Answer №1

When trying to access attributes on an array of objects like LogRepair, the error message may show square brackets after the type name.

If you're working with Angular, it's possible you intended to reference repair.timestamp within a loop instead of repairs.timestamp, which refers to the entire array.

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

Exploring the DynamoDB List Data Type

Currently, I am working on an angular 8 application where I have chosen to store JSON data in a list data type within DynamoDB. Inserting records and querying the table for data has been smooth sailing so far. However, I have run into some challenges when ...

Can Angular 4 experience race conditions?

Here is a snippet of my Angular 4 Service code: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Perform calculations and add results to myAr ...

Implementing a global provider in Ionic 3

I have integrated a provider in my app that needs to stay active at all times while the application is running to monitor the network connection status. Following this guide, I included the class in my app.module.ts file to ensure it functions as a global ...

"Encountered a 'Module not found: Error: Can't resolve' issue while attempting to install from GitHub. However, the standard installation process

I am currently utilizing the Quilljs JavaScript library for a project in Angular. After installing it with the following command: npm install --save quill Everything appears to be functioning correctly, and I am able to import the Quill class into my Ty ...

Create a configuration featuring filter options similar to Notion's functionality

The objective is to create a system that can establish certain constraints, similar to the way Notion handles filter properties. https://i.sstatic.net/plctW.png System A sets up the constraints and System C evaluates them, both using Typescript. However, ...

Error TS2307: Module 'calculator' could not be located

Running a Sharepoint Framework project in Visual Studio Code: This is the project structure: https://i.stack.imgur.com/GAlsX.png The files are organized as follows: ComplexCalculator.ts export class ComplexCalculator { public sqr(v1: number): number ...

Activate the spring profile for Angular2 development in order to enhance functionality

Our Web App utilizes Angular and Spring Boot technologies. We have implemented dev, test, and prod spring profiles to load varying properties based on the environment in which the application is running. This approach allows us to parametrize properties wi ...

How can I efficiently create an editForm in Angular?

Within my database, there are numerous users, each with their own collection of recipes. Each recipe contains various properties and a list of ingredients. Take a look at the screenshot below: Recipe with all properties When a user goes to edit a recipe ...

What could be causing the div to be wider than the content inside of it?

I am having an issue creating a webpage with a 20-60-20 flex fill layout. The div in the center, which should occupy 60% of the page, is wider than its content, causing it to appear off-center. https://i.stack.imgur.com/WwCJy.png Here is my home.componen ...

Typescript Routing Issue - This call does not match any overloads

Need assistance with redirecting to a sign-up page upon button click. Currently encountering a 'no overload matches this call' error in TypeScript. Have tried researching the issue online, but it's quite broad, and being new to Typescript ma ...

Error encountered during conversion to Typescript for select event and default value

When trying to set the defaultValue in a Select component, TSlint throws an error - Type 'string' is not assignable to type 'ChangeEvent<HTMLInputElement> | undefined - for the code snippet below: const App = () => { const [ mont ...

Tips for modifying only one property after receiving an object from an HTTP GET request

Within my Angular application, I have defined an object structure like this: export class Article { id: number; author: number; title: string; content: string; date: Moment; readingTime: number; draft: boolean; constructor(obj: Partial< ...

Can Angular i18n facilitate language switching?

My objective is to switch the language from English (United States) to Indonesia using a button. View Source Code https://i.sstatic.net/0YlfWaCY.gif The issue is that the tutorial does not cover how to implement the language change feature. Why opt for ...

Recover the node modules directory

By mistake, I deleted the MyProject/node_modules directory from my solution. Is there a way to recreate this folder? I attempted running npm install and npm update, but had no luck. ...

Flexible type definition including omission and [key:string]: unknown

When I write code, I like to explain it afterwards: type ExampleType = { a: string; b: boolean; c: () => any; d?: boolean; e?: () => any; [inheritsProps: string]: unknown; // If this ^ line over is removed, TypeNoC would work as expecte ...

I am having issues with npm starting properly as it is unable to locate the module 'worker_threads'

My current versions are node: v10.24.0 and npm: 6.14.11. When I try to run my Angular frontend using ng serve, I encounter the following error: ng serve An unhandled exception occurred: Cannot find module 'worker_threads' Check "/tmp/n ...

Tips for incorporating runtime configuration into an Angular module and effectively leveraging it

After setting up Apollo Angular, I encountered a challenge in src/app/graphql.module.ts src/app/graphql.module.ts import { NgModule } from '@angular/core'; import { APOLLO_OPTIONS } from 'apollo-angular'; import { ApolloClientOptions, I ...

What is the reason behind Angular triggering @HostListener as soon as the element begins to appear in the DOM?

I encountered an issue while writing a directive to close an element when a user clicks outside of the host element. The problem seems to be that the @HostListener is triggered immediately once the element is displayed in the DOM. It might not actually be ...

The Step-by-Step Guide to Deselecting an Angular Checkbox with a Button

I currently have a situation where I have three checkboxes labeled as parent 1, parent 2, and parent 3. Initially, when the page loads, parent 1 and parent 3 are checked by default, while parent 2 is unchecked. However, when I manually check parent 2 and c ...

How to implement a Typescript interface without necessarily implementing the parent interfaces

Within my current project, I have defined the following interfaces: interface foo { fooProperty: number; fooFunction(): void; } interface bar extends foo { barProperty: string; barFunction(): void; } Now, I am interested in creating a class like ...