【发布时间】:2020-10-07 15:31:11
【问题描述】:
我在执行 POST 请求时发送查询参数。我试过使用 req.query.mmCode 它一直在服务器端返回 undefined。
app.js
app.post("/api/risk/:id", (req, res) => {
// Create a new note and pass the req.body to the entry
var mmCode = req.query.mmCode;
console.log(mmCode);
service.ts
addRisk(params: HttpParams, id: string) {
console.log(params);
this.http
.post("http://localhost:3000/api/risk/" + id, { params })
.subscribe(responseData => {
console.log(responseData);
// this.vehicles.push(vehicle);
// this.vehicleUpdate.next([...this.vehicles]);
});
}
add.component.ts
onAddVehicle(form: NgForm) {
this.params = null;
if (form.invalid) {
return;
}
this.isLoading = true;
const vehicle: Vehicle = { id: null, mmCode: form.value.mmCode, vehicleType: form.value.vehicleType,
make: form.value.make, model: form.value.model, regYear: form.value.regYear};
this.http.post<{ message: string, vehicle: Vehicle }>("http://localhost:3000/api/vehicles", vehicle)
.subscribe((responseData) =>{
this.addVehicle = responseData;
this.addMMCode = this.addVehicle.vehicle.mmCode;
this.vehicleID = this.addVehicle.vehicle._id;
this.params = new HttpParams()
.set('mmCode', this.addMMCode);
console.log('CODE ' + this.addMMCode);
this.vehicleService.addRisk(this.params, this.vehicleID);
this.isLoading = false;
form.resetForm();
}
【问题讨论】:
-
mmCode在service.ts中的位置 -
你还没有在url中发送
mmCode -
我编辑了这个问题。我在 component.ts 中发送 mmCode
标签: node.js angular mongodb typescript