Node.js 检索查询后参数
我在执行POST请求时发送查询参数。我尝试过使用req.query.mmCode,但它在服务器端一直返回未定义 app.jsNode.js 检索查询后参数,node.js,angular,mongodb,typescript,Node.js,Angular,Mongodb,Typescript,我在执行POST请求时发送查询参数。我尝试过使用req.query.mmCode,但它在服务器端一直返回未定义 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); 服务台 addRisk(p
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);
服务台
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();
}
onAddVehicle(格式:NgForm){
this.params=null;
如果(格式无效){
返回;
}
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};
这是http.post(“http://localhost:3000/api/vehicles“,车辆)
.订阅((响应数据)=>{
this.addVehicle=响应数据;
this.addMMCode=this.addVehicle.vehicle.mmCode;
this.vehicleID=this.addVehicle.vehicle.\u id;
this.params=新的HttpParams()
.set('mmCode',this.addMMCode);
console.log('CODE'+this.addMMCode);
this.vehicleService.addRisk(this.params,this.vehicleID);
this.isLoading=false;
resetForm();
}
无法检索POST查询参数的原因是没有任何。HttpClient
的POST()
的定义大致如下:POST(url、requestBody、options)
。查询参数应作为选项传递,而不是body
。因此,在您的示例中(service.ts
):
addRisk(参数:HttpParams,id:string){
这是http
.post(“http://localhost:3000/api/risk/“+id,{params})
.订阅(响应数据=>{
控制台日志(responseData);
});
}
应该是
addRisk(参数:HttpParams,id:string){
这是http
.post(“http://localhost:3000/api/risk/“+id,null,{params})
.订阅(响应数据=>{
控制台日志(responseData);
});
}
HttpClient的文档:无法检索POST查询参数的原因是没有任何参数。HttpClient
的POST()
的定义大致如下:POST(url、请求主体、选项)
。查询参数应作为选项传递,而不是主体(service.ts
):
addRisk(参数:HttpParams,id:string){
这是http
.post(“http://localhost:3000/api/risk/“+id,{params})
.订阅(响应数据=>{
控制台日志(responseData);
});
}
应该是
addRisk(参数:HttpParams,id:string){
这是http
.post(“http://localhost:3000/api/risk/“+id,null,{params})
.订阅(响应数据=>{
控制台日志(responseData);
});
}
HttpClient的文档:此代码:
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.vehicleService.addRisk的作用,因此我们无法确定它是否合法
但正如前面的回答所指出的,除了从发送时获取查询参数外,没有其他方法(我知道)可以获取查询参数。帖子不会返回查询参数,而是返回正文数据。这就是为什么我更喜欢定义良好的对象来发送正文数据(可能包含带有查询参数的路由信息)。如果您的后端被训练为始终返回该字段,则所有设置都已设置。此代码:
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.vehicleService.addRisk的作用,因此我们无法确定它是否合法
但正如前面的回答所指出的,除了从发送时获取查询参数外,没有其他方法(我知道)可以获取查询参数。帖子不会返回查询参数,而是返回正文数据。这就是为什么我更喜欢定义良好的对象来发送正文数据(可能包含带有查询参数的路由信息)。如果您是后台用户,我们将培训您始终返回该字段,那么您已设置好。在服务中mmCode
的位置。ts
您尚未在URL中发送mmCode
。我编辑了该问题。我正在组件中发送mmCode。ts请查看服务中mmCode
的位置。ts
您尚未发送mmCode
在URL中我编辑了问题。我正在组件中发送mmCode。t请查看此内容