Ad

Expected 2-3 Arguments But Got 4 In Angular While Performing POST Operation?

- 1 answer

I have a form and when i click the submit button onSubmit() function is called and here i have printed in console all the datas that are needed to be passed through component class like this:

onSubmit() {
    this.submitted = true;
    console.log("entered in submiteed");
    console.log(this.selectedTable);
    console.log(this.documents);
    console.log(this.empList);

    this.letter.inOut="AnnexOne";
    this.letter.letterFile=null;
    this.letter.representativeName=null;
    this.letter.assessmentNo=0;
    this.letter.letterIssuedSubBy=null;
    console.log(this.letter);
      this.letterService.saveThree(this.selectedTable,this.documents,this.empList).subscribe(data => console.log(data), error => console.log(error));
  }

The JSON data that is printed in console is like:

enter image description here

I tried to send the three parameters from here saveThree(this.selectedTable,this.documents,this.empList)

and send it to service class,so i need to perform post operation so i tried :

export class LetterService {

  private baseUrl = 'http://localhost:8080/api/letter';
   private letter = new Letter();

  constructor(private http: HttpClient) { }


 saveThree(selectedTable: any,documents: any,empList: any): Observable<Object> {
    return this.http.post(`${this.baseUrl}` + `/create`, selectedTable,documents,empList);
  }

} 

In this class also i have used three parameters to receive but the error is :

enter image description here

I need to pass these three parameters from service class to backend but Why it is me showing me such mismatch error though there are equal number of parameters?

Ad

Answer

Because POST request has three parameters(first is URL,second is DATA that you need to pass and third is optional which is header information).If you want to pass more than one data then you need to like below.

return this.http.post(`${this.baseUrl}` + `/create`, {selectedTable,documents,empList})}
Ad
source: stackoverflow.com
Ad