【问题标题】:Patch API call not being called from the frontend未从前端调用补丁 API 调用
【发布时间】:2019-07-03 16:37:08
【问题描述】:

我的 http.patch 没有被调用到后端。这只发生在从前端调用它时。我已经在 Postman 中尝试过,效果很好。

  • 我已经单独尝试了后端,并且工作正常。
  • 我一直在到处进行控制台日志记录,得出的结论是前端未连接到后端(仅这条路线,其他路线都可以正常工作,我有大量的获取、发布、删除工作)
  • 我想我有一个 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();
}

学生服务.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 });
    });
});

问候。

【问题讨论】:

    标签: angular typescript rxjs angular6


    【解决方案1】:

    您需要订阅 http 调用,您可以通过将 tap 替换为 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();
    }
    

    希望对您有所帮助。

    【讨论】:

    • 如果我将 tap 更改为 switchMap 打字稿投诉,“类型参数 '(x: Internado) => void' 不能分配给类型参数 '(value: Internado, index: number) => ObservableInput'。类型 'void' 不可分配给类型 'ObservableInput。我认为这与第一次点击时的int 有关。这是来自自定义类型Internado
    • 一秒钟我会检查
    • 您是否像示例一样将 return 放入 switchMap 中? return this.estudiantesService.patchEstudiante(this.estudianteId, internado);
    • 天才。我想我还了解到您需要return 一个链式可观察对象。非常感谢!顺便说一句,我现在收到 500 错误,哈哈。但我现在就去。再次感谢:)
    • 没问题,我很高兴它有帮助!
    猜你喜欢
    • 2023-01-03
    • 1970-01-01
    • 2020-03-20
    • 2012-02-17
    • 2014-02-20
    • 1970-01-01
    • 2020-10-29
    • 2017-08-31
    • 2018-04-01
    相关资源
    最近更新 更多