2

I'm not too sure how to phrase the question in a better way, but I need some help in understanding how to resolve this. The following is my error:

  1. TypeError: _co.create is not a function
  2. TypeError: Cannot read property 'name' of undefined

When I try to use newCategory.name in the html, it throws these errors in the console. So I think the problem lies within the HTML.

newCategory is defined in CreateCategoryComponent.ts

newCategory: Category;
name: string;
description: string;

CategoryService.ts

 //ommitted some redundant code
 baseUrl: string = "api/Category";

 createCategory(newCategory: Category) : Observable<any> {

  //Not too sure if newCategory is added correctly here
  return this.httpClient.get<any>(this.baseUrl+"username="+this.sessionService.getUsername()+"&password="+this.sessionService.getPassword() + newCategory).pipe (
    catchError(this.handleError)
    );
  }

CreateCategory.html

<td><input id="name" name="name" #name="ngModel" type="text" [(ngModel)]="newCategory.name" required="true" /></td>
3
  • 1
    newCategory: Category; this only defines newCategory to be type of Category, but it doesn't initialize values Commented Apr 16, 2019 at 14:30
  • 1
    newCategory is declared inside your component, but not defined Commented Apr 16, 2019 at 14:30
  • @mxr7350 yes. I missed out that. Thank you. Needed to add this.newCategory = new Category(); Commented Apr 16, 2019 at 14:35

3 Answers 3

3

Nothing is wrong with your HTML.

The problem is newCategory: Category; as values are not initialized. All you are doing here is setting newCategory to be type of Category.

You can initialize newCategory on the following ways:

  1. Using new operator
       newCategory: Category = new Category();
  1. I usually declare initial state in Category model file and than import it into appropriate files.
    export const initialState: Category = {
      name: '',
      description: ''
    };
  1. Initialize values in the component
    newCategory: Category = {
      name: '',
      description: ''
    };
Sign up to request clarification or add additional context in comments.

Comments

2

Your component has 3 attributes:

  • newCategory: Category;
  • name: string;
  • description: string;

Then your needs set/get the attribute name, you'll use: name.

<td><input id="name" name="name" #name="ngModel" type="text" [(ngModel)]="name" required="true" /></td>

Or you needs add name to your newCategory object:

newCategory: Category = {
    name: string, 
    description: string
};

And use newCategory.name in your template:

<td><input id="name" name="name" #name="ngModel" type="text" [(ngModel)]="newCategory.name" required="true" /></td>

Hope this help!

Comments

0

This occurred for me when the page was loaded before the object was returned from an async call. My solutions was an *ngIf block.

<div *ngIf="object"><br />
<b>{{ object.property1}}</b><br />
<i>{{ object.property2}}</i><br />
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.