youssef11511
youssef11511

Reputation: 31

How to handle 404 exception handling in angular/Expressjs

I have 404 exceptions and I want to handle it I try something and is not work perfectly because I still have an error

please check the image

enter image description here

my objective is: I have a login and there is a type of users the responsible and his assistants and in the backend, I have two collections (I work with MongoDB Nodejs ionic expressjs) the first collection is for the responsible and the other one is for his assistants so in the login if the employee number is equal to responsible he will log in as a responsible and get admin access else he will be assistant

this is my code and I hope that you help me in my case I still have the get error that I want to solve :

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

here the backend method used :

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

here the service :

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

Upvotes: 0

Views: 346

Answers (2)

youssef11511
youssef11511

Reputation: 31

the problem was in the backend here the code :

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

I forget to add : in the get method

Upvotes: 0

Barremian
Barremian

Reputation: 31105

  1. You ought to use higher order mapping operator like switchMap instead of nested subscriptions. See here for more info.
  2. If all you wish to do is not show the error in the browser console, you could try to use the RxJS catchError operator.

Try the following

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

Upvotes: 1

Related Questions