按下删除键时出现内部服务器错误

时间:2019-12-01 20:06:13

标签: angular mean

ERROR Error Code: 500
Message: Http failure response for http://localhost:4000/api/update/5de40f285e9c793ed4af996c: 500 Internal Server Error

enter image description here

图像之一是api服务

enter image description here

第一段代码来自api服务,第二段代码是editcomponent

UpdateStudent(id, data: Student): Observable<any> {
        let API_URL = `${this.endpoint}/update/${id}`;
        return this.http.put(API_URL, data, { headers: this.headers }).pipe(
          catchError(this.errorMgmt)
        )
      }

Api服务代码

updateStudentForm() {
        console.log(this.studentForm.value)
        var id = this.actRoute.snapshot.paramMap.get('id');
        if (window.confirm('Are you sure you want to update?')) {
          this.studentApi.UpdateStudent(id, this.studentForm.value).subscribe(res => {
            this.ngZone.run(() => this.router.navigateByUrl('/students-list'))
          });
        }
      }

    }

1 个答案:

答案 0 :(得分:2)

如果您想看到自己的错误,可以使用catchError来捕获它,如下所示修改服务:

import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';

@Injectable()
export class StudentApiService {

  UpdateStudent(id, data: Student): Observable<any> {
        let API_URL = `${this.endpoint}/update/${id}`;

        return this.http.put(API_URL, data, { headers: this.headers }).pipe(
        tap(data => console.log("Daten:", data)),
        catchError(this.handleError),
      )
   }


  private setHttpHeader() {
    const headers = new HttpHeaders().set('Accept', 'application/json').set('Content-Type', 'application/json');
    let options = { headers: headers };
    return options;
  }

  private handleError(error: Response): Observable<any> {
    console.error("observable error: ", error);
    return Observable.throw(error.statusText);
  }

}

现在,您可以在浏览器->控制台标签中看到错误了

如果您收到状态码为500的错误:这意味着您的服务器中存在错误(检查您的API)