sebaLinares
sebaLinares

Reputation: 495

Patch API call not being called from the frontend

My http.patch is not being called to the backend. This only happens when calling it from the frontend. I already tried in in Postman and it works fine.

editar-estudiante.component.ts

submit() {
    let internado: Int;
    this.internadoService
      // get the object from database that has what i need
      // to create `internado` in the next `tap`
      .getInternado(this.idInternadoElegido)
      .pipe(
        tap(int => {
          // create an object to send in the next tap, 
          // this is the object that will patch
          internado = {
            idInternado: int._id,
            nombreInternado: int.datosAdmin.nombre.toString(),
            nombreCortoInternado: int.datosAdmin.nombreCorto.toString()
          };
        }),
        tap(x => {
          // this two values `internado` and `this.estudianteID
          // are ok, i checked them many times.
          console.log("internado es: ", internado);
          console.log("estudianteId es: ", this.estudianteId);
          this.estudiantesService.patchEstudiante(this.estudianteId, internado);
        })
      )
      .subscribe();
}

estudiante-service.ts

patchEstudiante(id: string, int: Int): Observable<any> {
  //this lines get log just fine.
  console.log("id: ", id);
  console.log("int: ", int);
  return this.http
    // i tried this same route with postman and works fine.
    .patch("http://localhost:3000/users/internado/" + id, int)
    .pipe(
      catchError(err => {
        console.error("GET failed", err);
        const msg =
          "No puedes modificar el estudiante ahora; por favor intenta más tarde";
        return throwError(msg);
      })
    );
}

Last file: my backend route for this. users.js

//push internado
router.patch("/internado/:id", (req, res, next) => {
  //this does not log in the node console, so I guess that
  // we don't even reach this point, that's why I think the
  // problem is not in the backend ... yet.
  console.log("backend");
  const id = req.params.id;
  const updateOps = {};
  for (const ops of req.body) {
    for (let prop in ops) {
      updateOps[prop] = ops[prop];
    }
    // updateOps[ops.propName] = ops.value;
  }
  console.log("updateops: ", updateOps);

  User.update(
    { _id: id },
    { $push: { "datosAcademicos.internados": updateOps } }
  )
    .exec()
    .then(result => {
      console.log(result);
      res.status(200).json(result);
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({ error: err });
    });
});

Regards.

Upvotes: 0

Views: 410

Answers (1)

Amir Arbabian
Amir Arbabian

Reputation: 3699

You need to subscribe on http call, you can do that by substituting tap to switchMap, for example:

submit() {
    let internado: Int;
    this.internadoService
      // get the object from database that has what i need
      // to create `internado` in the next `tap`
      .getInternado(this.idInternadoElegido)
      .pipe(
        tap(int => {
          // create an object to send in the next tap, 
          // this is the object that will patch
          internado = {
            idInternado: int._id,
            nombreInternado: int.datosAdmin.nombre.toString(),
            nombreCortoInternado: int.datosAdmin.nombreCorto.toString()
          };
        }),
        switchMap(x => {
          // this two values `internado` and `this.estudianteID
          // are ok, i checked them many times.
          console.log("internado es: ", internado);
          console.log("estudianteId es: ", this.estudianteId);
          return this.estudiantesService.patchEstudiante(this.estudianteId, internado);
        })
      )
      .subscribe();
}

Hope that helps.

Upvotes: 1

Related Questions