【问题标题】:Getting an internal server error upon delete being pressed按下删除时出现内部服务器错误
【发布时间】:2020-03-26 12:13:17
【问题描述】:
ERROR Error Code: 500
Message: Http failure response for http://localhost:4000/api/update/5de40f285e9c793ed4af996c: 500 Internal Server Error

图片一是api服务

第一个代码块来自 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'))
          });
        }
      }

    }

【问题讨论】:

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

标签: angular mean


【解决方案1】:

如果你想查看你的错误,你可以使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多