简体   繁体   中英

Error TypeError: cannot set property “name” of undefined ANGULAR

I get this error when trying to submit my form, it used to work fine, after a while on working on some other feature this apparently happened. Been looking around for hours and have not found a solution yet. I do not know why this is happening, I tried initializing the component in the constructor as well as in the ngoninit lifecycle, but it still did not work. I can not see why this error is occurring, everything is supposed to be working, would love if someone could help me.

This is the error I get;

ERROR TypeError: Cannot set property 'name' of undefined at RecipeService.searchRecipes (recipe.service.ts:43) at RecipeSearchComponent.onSubmit (recipe-search.component.ts:33) at RecipeSearchComponent_Template_form_ngSubmit_8_listener (recipe-search.component.html:10) at executeListenerWithErrorHandling (core.js:21772) at wrapListenerIn_markDirtyAndPreventDefault (core.js:21814) at SafeSubscriber.schedulerFn [as _next] (core.js:37027) at SafeSubscriber.__tryOrUnsub (Subscriber.js:183) at SafeSubscriber.next (Subscriber.js:122) at Subscriber._next (Subscriber.js:72) at Subscriber.next (Subscriber.js:49)

This is the component

import { Component, OnInit } from '@angular/core';
import { Validators, FormBuilder } from '@angular/forms';
import { RecipeService } from '../recipe.service';
import { Router } from '@angular/router';

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


    // initilization of the form
  searchForm = this.fb.group({
    name: ["", [Validators.required, Validators.minLength(3)]],
    minCalories: ["0", Validators.required],
    maxCalories: ["1500", Validators.required]
  });

  constructor(private recipeService: RecipeService, private router: Router, private fb: FormBuilder) {
  }

  ngOnInit(): void {
  }


  // submit function that calls the searchRecipes function from the recipe service
  onSubmit() {
    const minCalories = +this.searchForm.get("minCalories").value;
    const maxCalories = +this.searchForm.get("maxCalories").value;
    const name = this.searchForm.get("name").value;
    this.recipeService.searchRecipes(1, minCalories, maxCalories, name);
    this.router.navigate(["recipes"]);
  }
}

This is the html

<section class="search">
  <div class="search__box">
    <div class="search__box--wrapper">
      <h1 class="search__box--title">Search</h1>
      <p class="search__box--paragraph">We Find Your Dream Recipes</p>
    </div>
  </div>

  <div class="search__form--box">
    <form class="search__form" [formGroup]="searchForm" (ngSubmit)="onSubmit()">
      <div class="search__group search__group--title">
        <input
        placeholder="Enter Any Title..."
        class="search__input search__input--title"
        type="text"
        formControlName="name"
        required
        minlength="3"
        >

      <small
        class="search__input--title--small"
        [ngClass]="{'name--red': !searchForm['controls'].name.valid}"
        >
        Must be least 3 Characters long!
      </small>

        <label class="search__label search__label--title" for="title">Name</label>
      </div>
      <div class="search__calories--box">
        <div class="search__group search__group--calories">
          <input
          placeholder="Min Calories"
          type="number"
          class="search__input search__input--calories"
          formControlName="minCalories"
          required>
          <label class="search__label" for="minCalories">Min</label>
        </div>
        <div class="search__group search__group--calories">
          <input
          placeholder="Max Calories"
          type="number"
          class="search__input search__input--calories"
          formControlName="maxCalories"
          required
          >
          <label class="search__label" for="maxCalories">Max</label>
        </div>
      </div>
      <button type="submit" class="search__button" [disabled]="!searchForm.valid">Search</button>
    </form>
  </div>
</section>

try this in your ngOnInit

    this.searchForm = this.fb.group({
    name: ["", Validators.compose([Validators.required, Validators.minLength(3)])],
    minCalories: ["0", Validators.required],
    maxCalories: ["1500", Validators.required]
  });

I had an issue in the service, recipe service, so this had actually nothing to do with the form, sorry for the late answer. thanks for everyone who watched and tried.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM