我的http.patch没有被调用到后端。这仅在从前端调用时发生。我已经在Postman中尝试过了,效果很好。
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 });
});
});
致谢。
答案 0 :(得分:1)
您需要订阅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();
}
希望有帮助。