Unable to proceed with deployment due to the absence of the 'category' property in the '{}' type

Everything seems to be functioning properly - I can add and remove products, all with the properties I specified in the database. However, my deployment for production is being hindered by some errors that I'm encountering.

ERROR in src\app\admin\product-form\product-form.component.html(10,11): : Property 'title' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(27,13): : Property 'price' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(48,11): : Property 'category' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(66,11): : Property 'imageUrl' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(10,11): : Property 'title' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(27,13): : Property 'price' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(48,11): : Property 'category' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(66,11): : Property 'imageUrl' does not exist on type '{}'.

Does this mean that these properties are missing from the object 'product'?

However, every product in the database has these exact properties assigned to them.

So why am I encountering this error?

This is the component:

 import { Component, OnInit } from '@angular/core';
import { CategoryService } from '../../category.service';
import { ProductService } from '../../product.service';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/take';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
  categories$;
  product = {};
  id;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private categoryService: CategoryService,
    private productService: ProductService
  ) {
    this.categories$ = categoryService.getAll();

    this.id = this.route.snapshot.paramMap.get('id');
    if (this.id) {
      this.productService
        .get(this.id)
        .take(1)
        .subscribe(p => (this.product = p));
    }
  }

  save(product) {
    if (this.id) {
      this.productService.update(this.id, product);
    } else {
      this.productService.create(product);
    }
    this.router.navigate(['/admin/products']);
  }

  delete() {
    if (!confirm('Are you sure you want to delete?')) {
      return;
    }
    this.productService.delete(this.id);
    this.router.navigate(['/admin/products']);
  }

  ngOnInit() {}
}

This is the template:

<div class="row">
    <div class="col-md-6">

        <form #f="ngForm"
            (ngSubmit)="save(f.value)">

            <div class="form-group">
                <label for="title">Title</label>
                <input #title="ngModel"
                    [(ngModel)]="product.title"
                    name="title"
                    type="text"
                    id="title"
                    class="form-control"
                    required>
                <div class="alert alert-danger"
                    *ngIf="title.touched && title.invalid">Title is required.</div>
            </div>

            <div class="form-group">
                <label for="price">Price</label>
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="input-group-text">$</span>
                    </div>
                    <input #price="ngModel"
                        [(ngModel)]="product.price"
                        name="price"
                        type="number"
                        class="form-control"
                        aria-label="Amount (to the nearest dollar)"
                        required
                        [min]="0">
                    <div class="input-group-append">
                        <span class="input-group-text">.00</span>
                    </div>
                </div>
                <div class="alert alert-danger"
                    *ngIf="price.touched && price.invalid">
                    <div *ngIf="price.errors.required">Price is required.</div>
                    <div *ngIf="price.errors.min">Price must be equal to or greater than zero.</div>
                </div>
            </div>

            <div class="form-group">
                <label for="category">Category</label>
                <select #category="ngModel"
                    [(ngModel)]="product.category"
                    name="category"
                    id="category"
                    class="form-control"
                    required>
                    <option value=""></option>
                    <option *ngFor="let c of categories$ | async"
                        [value]="c.$key">
                        {{ c.name }}
                    </option>
                </select>
                <div class="alert alert-danger"
                    *ngIf="category.touched && category.invalid">Category is required.</div>
            </div>

            <div class="form-group">
                <label for="imageUrl">Image URL</label>
                <input #imageUrl="ngModel"
                    [(ngModel)]="product.imageUrl"
                    name="imageUrl"
                    type="text"
                    id="imageUrl"
                    class="form-control"
                    required
                    url>
                <div class="alert alert-danger"
                    *ngIf="imageUrl.touched && imageUrl.invalid">
                    <div *ngIf="imageUrl.errors.required">Image URL is required.</div>
                    <div *ngIf="imageUrl.errors.url">Image URL must be formatted correctly.</div>
                </div>
            </div>
            <button class="btn btn-primary">Save</button>
            <button type="button"
                class="btn btn-danger"
                (click)="delete()">Delete</button>
        </form>
    </div>

    <div class="col-md-6">
        <product-card [product]="product"
            [show-actions]="false"></product-card>
    </div>
</div>

The product variable is defined as follows:

export interface Product {
  $key: string;
  title: string;
  price: number;
  category: string;
  imageUrl: string;
}

I have looked at other similar posts like this one: TS2339: Property does not exist on type, but they seem to have slightly different issues.

Answer №1

By declaring the variable with type 'any', the issue can be resolved.

selectedProduct: any = {};

Answer №2

In order to properly initialize the variable product, you must declare it with a specific type like this: product: Product;

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

Maximum size of data that can be included with output files in a setup project for a Windows Form application developed in Visual Studio, as authorized

Currently, I am developing a Windows Form application using C# and facing an issue with handling 5-10GB of SWF files within the project. Unfortunately, when attempting to build these files along with the project output in the setup project, Visual Studio T ...

What is the process for transforming pagination numbers into Arabic numerals?

When working with pagination in my project, I have utilized both ant design and material ui. However, I encountered a problem when attempting to change the default Latin numbers to Arabic numbers. Despite trying ant design localization, I was unable to aff ...

Steps for modifying an element in an array using Javascript

Currently, I am a beginner in the realm of JavaScript and I am trying to build a todo-style application. So far, I have successfully managed to create, display, and delete items from an array. However, I am facing challenges when it comes to editing these ...

Steps for preloading a user prior to the page loading

Main Concern I currently have an Auth Provider set up in my application that wraps around the entire _app.tsx file. This allows me to utilize the "useAuth" hook and access the user object from any part of the app. However, I am facing an issue when using ...

What is the best way to reset the state in React prior to the render being called?

Whenever I click on a copy icon, a popup is displayed. This happens because the state "showPopup" is set to true upon clicking the copy icon, and the render function recognizes this and shows the popup. The popup will close automatically if I click anywher ...

Using Symfony2 to make an Ajax request in order to show a message based on a particular condition

As a novice in the realm of JavaScript and Ajax, I am struggling to find a way to display a message based on certain conditions. Let's consider a scenario where a user can adjust the quantity of a product they wish to purchase. While implementing thi ...

Acquiring the Auth0 authentication token

Currently, I am using the Angular SDK of Auth0 and everything seems to be functioning correctly except for retrieving the token. At the moment, I am manually obtaining the token from my dashboard. The method in Auth0Service called getAccessTokenSilently i ...

The customer opts to store all images indefinitely into the data stream

I have set up a node server that captures images from a webcam at regular intervals and sends them to the client using the Delivery.js node module. Upon monitoring the browser resources in Chrome development tools, it appears that each image sent is being ...

Generating a USA map with DataMaps in d3jsonData

I'm trying to create a basic US map using the DataMaps package and d3 library. Here's what I have attempted so far: <!DOCTYPE html> <html> <head> <title> TEST </title> <script src="https://d3js.org/d3.v5.js"> ...

What is the best way to rotate an image using AngularJS?

Hey there, I've got an image that I need help rotating. There are two buttons - left and right - that should rotate the image 45 degrees in opposite directions. I attempted to create a directive using the jquery rotate library, but it's not worki ...

Is it possible for a React blog to be included in search engine results?

As I work on building my blog using React, Node.js, Express, Sequelize, and other technologies, a question has arisen in my mind: Will search engines index my articles, or will only the homepage of my site be noticed? For instance, if I have an article ti ...

Mobile device users experiencing issues with jQuery scroll up and down functionality

Currently, I am utilizing jQuery to identify when a user is scrolling the mouse wheel up or down. Below is my code: $(window).bind('mousewheel DOMMouseScroll', function(event){ if (event.originalEvent.wheelDelta > 0 || event.originalEvent ...

Unable to display image using EJS and Multer

While working on my node.js application, I encountered an issue with rendering the uploaded image file. I have successfully integrated multer to handle file uploads, and the images are being stored in the correct folder. However, when trying to display the ...

Ways to apply the .not selector efficiently in jQuery

I have a situation with two separate divs, one named task1 and the other named task2. Each of these tasks contains panels with various names. Within task2, there is a duplicate name (Greg), who also belongs to the duplicate class. I'm trying to figure ...

Ways to customize the appearance of a react-chartist component

I recently created a React JS component that utilizes the amazing react ChartistGraph component. However, I've run into an issue where the default CSS of ChartistGraph seems to be conflicting with my custom styling. While there is plenty of documentat ...

Is there an option for keyPrefix in i18next?

For my current project, I am utilizing both i18next and react-i18next. One useful feature of using the useTranslation hook from react-i18next is the "keyPrefix" option, which helps in reducing code duplication. However, there are instances where I need to ...

I have encountered an issue while utilizing dynamic form functionality in Angular 7. The error message displayed is: "Error: Cannot find control with name: 'i'"

While working with Angular 7 dynamic forms, I encountered an error that I'm struggling to resolve. As a newcomer to Angular, this has been quite challenging for me. import { Component } from '@angular/core'; import {FormBuilder, FormArray} ...

Can you explain the purpose of this function on Google PlusOne?

Within the code snippet below: (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByT ...

Exploring numerous choices within a multi-select "category" search bar (web scraping)

Looking to scrape data from this French website using Python and the bs4 library: the content is in french :) . Specifically interested in extracting all possible values of a multi-select search bar named 'TYPE DE BIENS'. This type of search bar ...

Navigating directly to a page in ReactJS and Redux without needing to login if a token is found in the local storage

Currently in the main component, it is set to automatically redirect to the Login page. However, I want to implement a check to see if a token is already stored in the local storage. If a token exists, I want the application to skip the Login page and dire ...