0

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

2 Answers2

1
  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 => { }
);
ruth
  • 29,535
  • 4
  • 30
  • 57
  • friend I try your code but it's the same error – youssef11511 Apr 20 '21 at 12:44
  • I don't know but this error I can't handle it from the nodejs code – youssef11511 Apr 20 '21 at 12:44
  • I don't quite understand the question. From an overview atleast I see you are expecting a `responsible` parameter in the request from the client. Are you sending it? Please show the code for `this.Loginser.getResponsable()` function. – ruth Apr 20 '21 at 12:51
0

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