代码之家  ›  专栏  ›  技术社区  ›  sebaLinares

没有从前端调用修补程序API调用

  •  0
  • sebaLinares  · 技术社区  · 7 年前

    后台没有调用我的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();
    }
    

    试验服务.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);
          })
        );
    }
    

    最后一个文件:我的后端路由。 用户服务系统

    //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 回复  |  直到 7 年前
        1
  •  1
  •   Amir Arbabian    7 年前

    您需要订阅HTTP调用,可以通过替换 水龙头 开关图 例如:

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

    希望有帮助。