在Angular 6中,类型'void'的参数不能分配给类型'string'的参数

时间:2018-11-16 12:05:16

标签: angular typescript

我的组件

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

我的服务文件

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

错误:类型'void'的参数不能分配给'字符串'类型的参数。当我尝试在Angular 6中调用API时显示错误。请帮助我解决此错误。例如:let photo = this.details.photo; (它正常工作,但我想作为FormData发送)。

1 个答案:

答案 0 :(得分:0)

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

append的FormData函数返回void,因此基本上您的变量photo在这里是空的,并且您试图将void的值传递给candidateDetailsExperienced期望它是string

因此,更改类型为string的照片变量。我猜您可以改用this.details.photo

您可以中断此行

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

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