简体   繁体   中英

Argument of type 'void' is not assignable to parameter of type 'string' in Angular 6

Mycomponent

  onFileChangedForCV(event) {
  this.details.= event.target.files[0]
  }
candidateDetails(){

    let formData = new FormData();
    let photo= formData.append('profile_pic',this.details.photo);  
    let name=this.details.name;
    let age = this.details.age;
    let cv = this.details.cv;
    let experience = this.details.experience;
    let current_salary = this.details.current_salary;
    let expected_salary = this.details.expected_salary;
    let notice_period = this.details.notice_period;
    if (this.contentEditable){
     this.detaisservice.candidateDetailsExperienced(photo(Here throwing ERROR),name,age,cv,experience,current_salary,expected_salary,notice_period)
      .subscribe(
        data => {
            this.details.DetailsResponse = data;
               if(this.details.DetailsResponse.code =='200'){ 
                 }
        },
        error => alert(error),
        () => console.log(this.details.DetailsResponse)
      );
    }
   }

MyService File

 candidateDetailsFresher(photo:string,name:string,age:string,cv:string){
      let body = JSON.stringify({photo:photo,name:name,age:age,cv:cv});
      let headers = new Headers({ 'Content-Type': 'application/json'});
      let options = new RequestOptions({ headers: headers });
      console.log(body);
      console.log(options);
     return this.http.post('http://127.0.0.1:8000/api/seeker/candidate_details/', body, options)
     .map(this.extractData)
     .catch(this.handleError);
   }
  private extractData(res: Response) {
      let body = res.json();
      return body || [];   
    }
    private handleError (error: any) {
      return Observable.throw(error.json().error || 'Server error');
    }

Error:Argument of type 'void' is not assignable to parameter of type 'string'. It's showing error when i am trying to call API in Angular 6 .Please help me to resolve this error.Ex:let photo = this.details.photo; (it working but i want to Send as FormData).

The issue with

let photo= formData.append('profile_pic',this.details.photo);  

append function of FormData returns void so basically your variable photo is void here and you are trying to pass the void value to candidateDetailsExperienced however it is expecting it to be string .

So change your photo variable of type string . I guess you can use this.details.photo instead.

You can break this line

let photo= formData.append('profile_pic',this.details.photo);

to

formData.append('profile_pic',this.details.photo);
let photo  = this.details.photo; //<-- you can change to other string value if reuqired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM