0

I am sending query parameters when doing a POST request. I have tried using req.query.mmCode it keeps returning undefined on the server side.

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

2 Answers2

1

The reason you can't retrieve POST query parameters is because there aren't any. HttpClient's post() is defined roughly as follows: post(url, requestBody, options). Query params should be passed as options, instead of body. So, in your example (service.ts):

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

should be

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

HttpClient's docs: Ref

1

This code:

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

Does not follow Single Responsibility Principle, it's trying to do something else after the response. If this is what you want then it should be moved to another function. We don't know what this.vehicleService.addRisk does so we can't say for sure if it's even legitimate.

But as the prior answer indicated, there are no ways (I know) to get the query parms other than from when you send them. Posts don't send back query parms they send back body data. This is why I prefer well-defined objects to send body data (which could contain the route information with query parms). If you're backend is trained to always return that field you are all set.

JWP
  • 5,969
  • 3
  • 39
  • 65