'string'类型的参数不能赋值为'string []'类型的参数

问题描述 投票:0回答:1

我一直在网上寻找解决方案,这可能是一个小问题。

我的功能就是一个。文件:

public getHelpContent(question: string[]) {

  let theHelp: any[] = [];
  theHelp = this.mssqlService.getTheHelpAnswer(question);
  console.log("THE HELP:", theHelp);
  this.commentContent = theHelp ;

  let foundContent: any[] = [];
  for (let i = 0; i < this.commentContent.length; i++) {
    let hitContent: string[];
    hitContent = this.searchHelpItem(question, this.commentContent[i]);
    if (hitContent) {
      foundContent.push(hitContent);
    }
  }

  return theHelp;

}

mssql-service.ts文件中的函数

getTheHelpAnswer(jsonObj: any): any {
  let body = JSON.stringify(jsonObj);
  console.log("BODY VAR: ", body);
  return this.http
    .post<any>(`${this.urlLocation}notification/TheHelp`, body)
    .map((response: Response) => response.json())
    .catch(this.handleError);
}

和繁荣的手柄错误......

private handleError(error: Response | any) {

  let errMsg: string;
  if (error instanceof Response) {
    const body = error || '';
    const err = JSON.stringify(body);
    errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Observable.throw(errMsg);

}

我编译的错误是这样的:

error TS2345: Argument of type 'string' is not assignable to parameter of type 'string[]'

当我改变:任何来自:

getTheHelpAnswer(jsonObj: any): any { 

getTheHelpAnswer(jsonObj: any): Observable<any> { ...

这是我得到的错误:

ERROR in src/app/app-help-state-machine.ts(364,5): error TS2322: Type 'Observable<any>' is not assignable to type 'any[]'.
  Property 'includes' is missing in type 'Observable<any>'.
src/app/app-help-state-machine.ts(371,50): error TS2345: 
Argument of type 'string' is not assignable to parameter of type 'string[]'.

我不明白问题是什么。

UPDATE1:

我忘了添加功能的签名

public getHelpContent(question: string[]): any[] { ...

但我仍然得到这个......

ERROR in src/app/app-help-state-machine.ts(364,5): error TS2322: Type 
'Observable<any>' is not assignable to type 'any[]'.
      Property 'includes' is missing in type 'Observable<any>'.
src/app/app-help-state-machine.ts(371,50): error TS2345: Argument of type 'string' is not assignable to parameter of type 'string[]'.

更新2:

我想到了...

看到我回答自己问题的解决方案....

typescript observable
1个回答
0
投票

这是解决方案:

我改变了这个功能:

public getHelpContent(question: string[]) {

  let theHelp: any[] = [];
  theHelp = this.mssqlService.getTheHelpAnswer(question);
  console.log("THE HELP:", theHelp);
  this.commentContent = theHelp ;

  let foundContent: any[] = [];
  for (let i = 0; i < this.commentContent.length; i++) {
    let hitContent: string[];
    hitContent = this.searchHelpItem(question, this.commentContent[i]);
    if (hitContent) {
      foundContent.push(hitContent);
    }
  }

  return theHelp;

}

对此:

public getHelpContent(question: string[]): string[] {

  const questionInfo = {
    "question": question
  }

  let theHelp = this.mssqlService.getTheHelpAnswer(questionInfo, question);
  console.log("THE HELP:", theHelp);

//Commented this out... as it caused the issue

//    this.commentContent = theHelp;
//
//    let foundContent: any[] = [];
//    for (let i = 0; i < this.commentContent.length; i++) {
//      let hitContent: string[];
//      hitContent = this.searchHelpItem(question, this.commentContent[i]);
//      if (hitContent) {
//        foundContent.push(hitContent);
//      }
//    }

  return lucyHelp;

}

并在mssql-connect.service.ts文件中,接收函数

改变这个:

getTheHelpAnswer(jsonObj: any): any {
   let body = JSON.stringify(jsonObj);
   console.log("BODY VAR: ", body);
   return this.http
       .post<any>(`${this.urlLocation}notification/TheHelp`, body)
       .map((response: Response) => response.json())
       .catch(this.handleError);
}

对...

getTheHelpAnswer(jsonObj: any, question: string[]): string[] {
   let body = JSON.stringify(jsonObj);
   console.log("BODY VAR: ", body);

   let result = this.http
     .post<any>(`${this.urlLocation}notification/TheHelp${question}`, body)
     .map((response: Response) => response.json())
     .catch(this.handleError);

   console.log("RESULT FROM HTTP: ", result);

  return result ;
}
© www.soinside.com 2019 - 2024. All rights reserved.