web-dev-qa-db-fra.com

Angular 6: ERREUR TypeError: Impossible de lire la propriété 'toLowerCase' de undefined

Je crée l'application CRUD dans angular six sur les utilisateurs, lorsque j'essaie d'ajouter un nouvel utilisateur, j'obtiens l'erreur suivante:

Voici l'erreur que j'obtiens:

core.js:12301 ERROR TypeError: Cannot read property 'toLowerCase' of undefined
    at HttpXsrfInterceptor.Push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http.js:1718)
    at HttpInterceptorHandler.Push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http.js:1134)
    at HttpInterceptingHandler.Push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http.js:1769)
    at MergeMapSubscriber.project (http.js:974)
    at MergeMapSubscriber.Push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:61)
    at MergeMapSubscriber.Push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:51)
    at MergeMapSubscriber.Push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at Observable._subscribe (scalar.js:5)
    at Observable.Push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:43)
    at Observable.Push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:29)

Voici ajouter le composant utilisateur html:

<div class="container add-user">
  <div class="col-md-6">
    <h2 class="text-center">Add User</h2>
    <form [formGroup]="addForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label for="email">Email address:</label>
        <input type="email" formControlName="email" placeholder="Email" name="email" class="form-control" id="email">
      </div>

      <div class="form-group">
        <label for="firstName">First Name:</label>
        <input formControlName="firstName" placeholder="First Name" name="firstName" class="form-control" id="firstName">
      </div>

      <div class="form-group">
        <label for="lastName">Last Name:</label>
        <input formControlName="lastName" placeholder="Last name" name="lastName" class="form-control" id="lastName">
      </div>

      <button class="btn btn-success">Update</button>
    </form>
  </div>
</div>

Voici le fichier ts:

import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {UserService} from '../service/user.service';
import {first} from 'rxjs/operators';
import {Router} from '@angular/router';

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

  constructor(private formBuilder: FormBuilder, private router: Router, private userService: UserService) { }

  addForm: FormGroup;

  ngOnInit() {

    this.addForm = this.formBuilder.group({
      id: [],
      email: ['', Validators.required],
      firstName: ['', Validators.required],
      lastName: ['', Validators.required]
    });

  }

  onSubmit() {
    this.userService.createUser(this.addForm.value)
      .subscribe( data => {
        this.router.navigate(['list-user']);
      });
  }

}

voici le service utilisateur:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { User } from '../model/user.model';

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

  constructor(private http: HttpClient) { }
  baseUrl: 'http://locahost:4200/users';

  getUsers() {
    return this.http.get<User[]>(this.baseUrl);
  }
  getUserById(id: number) {
    return this.http.get<User>(this.baseUrl + '/' + id);
  }
  createUser(user: User) {
    return this.http.post(this.baseUrl, user);
  }
  updateUser(user: User) {
    return this.http.put(this.baseUrl + '/' + user.id, user);
  }
  deleteUser(id: number) {
    return this.http.delete(this.baseUrl + '/' + id);
  }
}

J'ai essayé différentes manières mais je ne suis pas en mesure de résoudre le problème :)

qu'est-ce que je fais mal dans mon application? toute aide sera appréciée, merci

5
user9964622

changer la

baseUrl: 'http://locahost:4200/users';

à

baseUrl= 'http://locahost:4200/users';

puisque le type de baseUrl est une chaîne

4
h_w