【问题标题】:How to handle 404 exception handling in angular/Expressjs如何在 Angular/Expressjs 中处理 404 异常处理
【发布时间】:2021-07-14 15:36:32
【问题描述】:

我有 404 个异常,我想处理它我尝试了一些方法,但由于仍然有错误,所以无法正常工作

请查看图片

我的目标是:我有一个登录名,并且有一类用户负责人和他的助手,在后端,我有两个集合(我使用 MongoDB Nodejs ionic expressjs)第一个集合是负责人和他的另一个是他的助理,所以在登录时,如果员工编号等于负责人,他将以负责人身份登录并获得管理员访问权限,否则他将成为助理

这是我的代码,我希望你能在我的情况下帮助我,我仍然有我想要解决的 get 错误:

 this.Loginser.getdata(this.Form.value.DECAFFE).subscribe(res => {
      this.employee = res as Drafbies
      this.Loginser.getResponsable(this.employee.DECAFFE).subscribe(re => {
        console.log("here the responsible" + re)

      }, err => {
        if (err.status == 404) {
          console.log("is not responsible")
        }
         })
      console.log(res);
    })

这里使用的后端方法:

router.get("/responsible", async (req, res) => {
    const inv_exerc = await INV_EXERC.findOne({ RESPONSABLE: req.params.responsible })
        res.send(inv_exerc)
})

这里是服务:

 private readonly url = "http://localhost:3000/auth";
  private readonly url2 = "http://localhost:3000/drafbie";
  private readonly url3 = "http://localhost:3000/inv_exerc";
  private readonly url4 = "http://localhost:3000/inv_agent";
  constructor(private http: HttpClient) { }
  login(data):Observable<any> {
    return this.http.post(this.url, data);
  }
  getdata(data):Observable<any> {
    return this.http.get(this.url2 + `/${data}`);
  }
  getResponsable(responsible): Observable<any>{
    return this.http.get(this.url3+`/${responsible}`)
  }
  getAgent(agent): Observable<any>{
    return this.http.get(this.url4+`/${agent}`)
  }

【问题讨论】:

    标签: node.js angular express ionic-framework


    【解决方案1】:
    1. 您应该使用像switchMap 这样的高阶映射运算符,而不是嵌套订阅。请参阅here 了解更多信息。
    2. 如果您只想在浏览器控制台中不显示错误,您可以尝试使用 RxJS catchError 运算符。

    试试下面的

    import { of, NEVER } from 'rxjs';
    import { switchMap, catchError } from 'rxjs/operators';
    
    this.Loginser.getdata(this.Form.value.DECAFFE).pipe(
      switchMap(res => {
        console.log(res);
        this.employee = res as Drafbies;
        return this.Loginser.getResponsable(this.employee.DECAFFE)
      }),
      catchError((error: any) => {
        if (error.status == 404) {
          console.log("is not responsible");
          return NEVER;    // <-- do not emit on 404 Not Found
        }
        return of(error);  // <-- forward the error on other statuses
      })
    ).subscribe(
      re => {
        console.log("here the responsible" + re);
      },
      err => { }
    );
    

    【讨论】:

    • 朋友,我试试你的代码,但还是一样的错误
    • 我不知道但是这个错误我无法从nodejs代码处理它
    • 我不太明白这个问题。至少从概述中,我看到您在来自客户端的请求中期待 responsible 参数。你要寄吗?请显示this.Loginser.getResponsable()函数的代码。
    【解决方案2】:

    问题出在后端代码中:

    router.get("/:responsible", async (req, res) => {
        const inv_exerc = await INV_EXERC.findOne({ RESPONSABLE: req.params.responsible })
            res.send(inv_exerc)
    })

    我忘记在get方法中添加:

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 2019-06-04
      • 2019-05-02
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多