【问题标题】:Retrieve POST query parameters检索 POST 查询参数
【发布时间】: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();
    
  }

【问题讨论】:

标签: node.js angular mongodb typescript


【解决方案1】:

您无法检索 POST 查询参数的原因是没有任何参数。 HttpClientpost()大致定义如下:post(url, requestBody, options)。查询参数应作为options 传递,而不是body。所以,在你的例子中(service.ts):

addRisk(params: HttpParams, id: string) {
    this.http
        .post("http://localhost:3000/api/risk/" + id, { params })
        .subscribe(responseData => {
            console.log(responseData);
        });
}

应该是

addRisk(params: HttpParams, id: string) {
    this.http
        .post("http://localhost:3000/api/risk/" + id, null, { params })
        .subscribe(responseData => {
            console.log(responseData);
        });
}

HttpClient 的文档:Ref

【讨论】:

    【解决方案2】:

    这段代码:

     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 做了什么,所以我们不能确定它是否合法。

    但正如先前的答案所示,除了发送查询参数之外,没有其他方法(我知道)获取查询参数。帖子不会发回查询参数,它们会发回正文数据。这就是为什么我更喜欢定义明确的对象来发送正文数据(可能包含带有查询参数的路由信息​​)。如果您的后端被训练为始终返回该字段,那么您就一切就绪。

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 2019-01-17
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多