The error message "Property 'name' does not exist on type 'User'" is encountered

When running this code, I expected my form to display in the browser. However, I encountered an error:

Error: src/app/addproducts/addproducts.component.html:18:48 - error TS2339: Property 'price' does not exist on type 'ADDPRODUCTSComponent'.
    
    18     <input type="text" id="price" [(ngModel)]="price" #client="ngModel" name="price" placeholder="Enter Price..">
                                                      ~~~~~
    
      src/app/addproducts/addproducts.component.ts:7:16
        7   templateUrl: './addproducts.component.html',
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component ADDPRODUCTSComponent.
    
    
    Error: src/app/addproducts/addproducts.component.html:18:48 - error TS2339: Property 'price' does not exist on type 'ADDPRODUCTSComponent'.
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component ADDPRODUCTSComponent.

user.ts

export class User{

    constructor(
                name:string,
                price:number
                ){}
    
    }   

addproducts.component.html

<!DOCTYPE html>
<html>

<body>

<h3>Using CSS to style an HTML Form</h3>

<div style="text-align: center">
    <h1>{{message}}</h1>
</div>

<div>
  <form action="/action_page.php">
    <label for="name">First Name</label>
    <input type="text" id="name" [(ngModel)]="user.name" #client="ngModel" name="name" placeholder="Your Product name..">

    <label for="price">Price</label>
    <input type="text" id="price" [(ngModel)]="user.price" #client="ngModel" name="price" placeholder="Enter Price..">

   

  
    <input type="submit" value="Submit" (click)="registerNow()">
  </form>
</div>

</body>
</html>

addproducts.component.ts

import { Component, OnInit } from '@angular/core';
import { ProductDetailsService } from '../product-details.service';
import {User} from '../user';

@Component({
  selector: 'app-addproducts',
  templateUrl: './addproducts.component.html',
  styleUrls: ['./addproducts.component.css']
})
export class ADDPRODUCTSComponent implements OnInit {


user:User = new User("",0);
message:any;

  constructor(private serivce:ProductDetailsService ) { }

  ngOnInit(): void {
  }
 
public registerNow(){

  let res=this.serivce.doRegistration(this.user)

  res.subscribe((data)=>this.message=data)



}

}

app-routing.modules.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ADDPRODUCTSComponent } from './addproducts/addproducts.component';
import { OperationComponent } from './operation/operation.component';

const routes: Routes = [

  {path:"",redirectTo:"register",pathMatch:"full"},
  {path:"register",component:ADDPRODUCTSComponent},
  {path:"operation", component:OperationComponent}

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

product-details.service.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

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

  constructor(private http:HttpClient) { }

  public doRegistration(user){

    return this.http.post("http://localhost:8084/add",user,{responseType:'text' as 'json'})
  }
}

Answer №1

The issue lies in what you haven't done rather than what you are doing wrong. Let's focus on the error at hand:

There is an absence of a 'price' property on the 'ADDPRODUCTSComponent' type.

Now, let's take a closer look at ADDPRODUCTSComponent:

export class ADDPRODUCTSComponent implements OnInit {

  user:User = new User("",0);
  message:any;

  constructor(private serivce:ProductDetailsService ) { }

  ngOnInit(): void {
  }
 
  public registerNow(){
    let res=this.serivce.doRegistration(this.user)
    res.subscribe((data)=>this.message=data)
  }
}

Is the price property present in the component class? Whenever you bind to a property in the HTML, that property must be declared within the component class.

To resolve this problem, all you need to do is add the missing property to the component:

export class ADDPRODUCTSComponent implements OnInit {
  price: any;  // <--- Added
  
  user:User = new User("",0);
  message:any;

  constructor(private serivce:ProductDetailsService ) { }

  ngOnInit(): void {
  }
 
  public registerNow(){
    let res=this.serivce.doRegistration(this.user)
    res.subscribe((data)=>this.message=data)
  }
}

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

Having difficulties viewing the sidemenu icon on Ionic 3, despite enabling the menu through MenuController

I am trying to show the sidemenu icon on my Home page, which users can access from the Add-Contract page. Even though I have enabled the sidemenu in home.ts using this.menu.enable(true);, the icon is not visible. However, I can still swipe and access the ...

Conceal dynamically generated div elements created with ngIf

I am currently working on initializing this div using ngOnInit in Angular ngOnInit(): void { let optTemp = ''; for (let data of arrOption) { optTemp = optTemp + '<option>' + data.trim() + '</option> ...

Displaying data from a service on an Ionic screen: a comprehensive guide

I've retrieved JSON data from an API: { "userGroups":[ {"title":"group 1"}, {"title":"group 2"}, {"title":"group 3"}, {"title":"group 4"} ] } Currently, I am storing this raw data in NativeStorage. // userService this.userGroup ...

Retrieve the duplicated items from an array by comparing two specific properties in JavaScript

I need assistance in identifying and retrieving duplicate objects within an array that share similarities in 2 specific properties. Consider the object structure below: let arry = [ {Level: "A-1", Status: "approved"}, {Level: &q ...

Employing ngModel within an (click) event in Angular 4

I have this html code snippet: <div class="workflow-row"> <input type="checkbox" id="new-workflow" [(ngModel)]="new_checkbox"> <label>New Workflow</label> <input type="text" *ngIf="new_checkbox" placeholder="Enter ...

Exploring the Nested JSON Data Loop with *ngFor in Angular 5/4

Recently I started working with Angular, and I've created a service to iterate over nested JSON data for my list. export const CATEGORIES: Category[] = [ { id: 1, categoryName:'Accessories', subcatName: [ {subcategory: & ...

What is the best way to initialize a discriminated union in TypeScript using a given type?

Looking at the discriminated union named MyUnion, the aim is to invoke a function called createMyUnionObject using one of the specified types within MyUnion. Additionally, a suitable value for the value must be provided with the correct type. type MyUnion ...

Emphasizing the chosen element in a list using angular

After retrieving an array of items from the database, my list is constantly changing, causing the HTML display to update dynamically. However, I am struggling with highlighting only the selected item in the list, as the entire list gets highlighted upon se ...

Using placeholders with inputs in an Angular2 table generated by ngFor

I have an array public example = [['hallo', 'fruit', 'rose'], ['apple','book']] Currently, I am working on creating a table of inputs. The values in this table depend on the specific part that I am usin ...

Adjusting the timing of a scheduled meeting

Is there a way for me to update the time of a Subject within my service? I'm considering abstracting this function into a service: date: Date; setTime(hours: number, mins: number, secs: number): void { this.date.setHours(hours); this.date.s ...

Issue updating @angular/core in Angular CLI caused by rxjs dependency

Currently, I am in the process of updating angular and angular material to version 6. I have successfully updated the cli to allow for the new ng update command. However, when attempting to use it to update @angular/core, I encounter an error stating that ...

Select a random class from an array of classes in JavaScript

I have a collection of Classes: possibleEnemies: [ Slime, (currently only one available) ], I am trying to randomly pick one of them and assign it to a variable like this (all classes are derived from the Enemy class): this.enemy = new this.possibleEn ...

You cannot assign an array of 'Contact' objects to a single 'Contact' parameter

During the development process, I encountered an error while trying to add a new contact. The error message states: Error: src/app/contacts/contacts.component.ts:32:28 - error TS2345: Argument of type 'Contact[]' is not assignable to parameter o ...

Troubles with Conditional Application of CSS Styles to a Twitter-Bootstrap Table

I am facing an issue with highlighting a row in a table when a barcode is entered. Despite following similar scenarios and checking my code, the CSS doesn't seem to be applied. Can anyone help me figure out what I'm missing or how I can fix this? ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

What could be causing text to not appear when a button is clicked with Observable in Angular 2?

I am experiencing an issue with my code that is intended to display the string representation of data using an Observable upon clicking a button. Below is the code snippet (Plunker link: https://plnkr.co/edit/wk3af4Va2hxT94VMeOk9?p=preview): export class ...

Deactivate the underscore and include the fiscal year in AngularJS

I am currently faced with a scenario where the back end is returning the value as follows: 123222_D1.123 However, I need to display the date from the database (12-Jun-2020) as 2020-D1.123 in a drop-down menu. Currently, I am displaying the above value i ...

Ways to access the new value of a cell in a Material UI Datagrid through the use of GridCellParams

When trying to access the newly modified value in a cell using the GridCellParams interface, I am faced with the issue that the 'value' property only provides me with the previous value of the cell. This asynchronous behavior is not ideal for my ...

While attempting to reinstall the admob-free plugin via npm, I encountered an error stating that it was missing a package.json file

While developing an app using Ionic, I encountered an issue with the AdMob plugin not being installed correctly. Trying to resolve this, I attempted to reinstall the plugin multiple times but kept running into errors. Seeking help from various threads, I ...

From where does the require.js file originate?

Currently, I am learning Angular 2 from the tutorial available at https://github.com/pkaul/maven-typescript-example. Once I proceed with the 3rd step (mvn jetty:run), a runnable war folder is generated in the example-webapp/target directory. However, I ha ...