angular 在 HttpErrorResponse 后停止工作

angular stops working after HttpErrorResponse

我正在使用 angular 8 和 NodeJS 服务器,mongo 作为我的数据库。 我想问问客户,如果他想继续,他是否在没有完成购物车的情况下就登录了 或启动一个新的购物车。问题是当将购物车状态从 'active: true' 变为 'active: false' 并验证 NodeJS 无法再调用此购物车时,我在服务调用后进入 angular 我得到一个 HttpErrorResponse .subscribe() 中的所有内容都不起作用。

nodeJs路由

 router.get("/getCartDetails", async (req, res, next) => {
try {
    const userId = ObjectID(req.headers.user._id);
    let cart = await Cart.findOne({ userId, active: true });
    if (!cart) return res.status(404).send("no active carts were found"); // no matter what I send I get an HttpErrorResponse 404 //
    res.status(201).send(cart);
} catch (err) {
    console.log(err);
    res.status(404).send("no active carts were found");
}

});

angular 组件中的代码部分

enter code here
 this.cartService.getCartDetails().subscribe(result => { // nothing works in subscribe//
        if (result) {
          this.cartID = res._id;
          this.modalService.open(this.content);
        } else {
          this.router.navigate(["/products"])
        }      
      })

购物车服务代码部分

getCartDetails(): Observable<any> {
return this.http.get(`${baseUrl}/cart/getCartDetails`)

}

您正在明确发送 404 状态。在这种情况下,HTTP observable 将发出 error 通知而不是 next 通知。并且目前订阅中没有错误处理。

尝试在订阅中添加error回调

this.cartService.getCartDetails().subscribe({
  next: (result: any) => {
    if (result) {
      this.cartID = res._id;
      this.modalService.open(this.content);
    } else {
      this.router.navigate(["/products"])
    }      
  },
  error: (error: any) => {
    console.log('Got error:', error);
    // do what you want when an error is emitted
  }
});