Вызов API Patch не вызывается из внешнего интерфейса - PullRequest
0 голосов
/ 09 февраля 2019

Мой http.patch не вызывается на сервер.Это происходит только при вызове с внешнего интерфейса.Я уже пробовал в Почтальоне, и он отлично работает.

  • Я пробовал только один бэкэнд, и работает нормально.
  • Я везде входил в консоль и пришел к выводу, что веб-интерфейс не подключается к бэкэнду (только этот маршрут, другие маршруты работают нормально, у меня есть тонны get, post, delete этой работы)
  • Мне кажется, у меня проблема с оператором rxjs

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

Последний файл: мой внутренний маршрут для этого.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 });
    });
});

С уважением.

1 Ответ

0 голосов
/ 09 февраля 2019

Вам нужно подписаться на http-вызов, вы можете сделать это, подставив нажмите на switchMap , например:

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

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...