简体   繁体   English

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

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

Mycomponent 为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 MyService文件

 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'. 错误:类型“ void”的参数无法分配给类型“ 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; 当我尝试在Angular 6中调用API时显示错误。请帮助我解决此错误。例如:let photo = this.details.photo; (it working but i want to Send as FormData). (它的工作,但我想作为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 . FormData的append函数返回void因此,基本上,您的可变photo在此处为void,并且您正在尝试将void值传递给candidateDetailsExperienced但是期望它是string

So change your photo variable of type string . 因此,更改您的string类型的照片变量。 I guess you can use this.details.photo instead. 我想您可以改用this.details.photo

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular - '(error: any) =&gt; void' 类型的参数不可分配给'() =&gt; void' 类型的参数 - Angular - Argument of type '(error: any) => void' is not assignable to parameter of type '() => void' 类型“ void”的参数不能分配给“功能”类型的参数 - Argument of type 'void' is not assignable to parameter of type 'Function' 类型“ void”的参数不能分配给类型数组的参数 - Argument of type 'void' is not assignable to parameter of type array 角4-string []类型的参数不能分配给&#39;string&#39;类型的参数 - Angular 4 - argument of type string[] is not assignable to type parameter of type 'string' &#39;string | 类型的参数 null&#39; 不能分配给类型为 &#39;string&#39; 的参数。 类型 &#39;null&#39; 不能分配给类型 &#39;string&#39;。 角 12 - Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'. Angular 12 角度-类型参数不能分配给参数 - Angular - Argument of type is not assignable to parameter Angular - 'OperatorFunction 类型的参数<iuser, void> ' 不能分配给 'OperatorFunction 类型的参数<object, void> '</object,></iuser,> - Angular - Argument of type 'OperatorFunction<IUser, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>' 如何修复&#39;类型&#39;数据的参数&#39;不能分配给Angular中&#39;string&#39;类型的参数? - How to fix 'Argument of type 'Data' is not assignable to parameter of type 'string' in Angular? “对象”类型的参数不可分配给“字符串”类型的参数 - 离子 Angular - Argument of type ‘Object’ is not assignable to parameter of type ‘string’ - Ionic Angular “订阅”类型的参数不能分配给“字符串”类型的参数。 角 - Argument of type 'Subscription' is not assignable to parameter of type 'string'. Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM