web-dev-qa-db-ja.com

Angular 6:エラーTypeError:未定義のプロパティ 'toLowerCase'を読み取れません

angular 6人のユーザーについてCRUDアプリを作成しています。新しいユーザーを追加しようとすると、次のエラーが発生します。

ここに私が得ているエラーがあります:

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)

ユーザーコンポーネント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>

ここに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']);
      });
  }

}

ここにユーザーサービスがあります:

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);
  }
}

別の方法を試しましたが、問題を解決できません:)

アプリで何が悪いのですか?どんな助けも感謝します、ありがとう

5
user9964622

変更

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

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

baseUrlのタイプは文字列であるため

4
h_w