Аргумент типа 'string' не может быть назначен параметру типа 'string []' - PullRequest
0 голосов
/ 02 апреля 2019

Я искал в Интернете решение этой, что, вероятно, незначительной проблемы.

Моя функция на один тс.file:

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[]'.

Я не понимаю, в чем проблема.

ОБНОВЛЕНИЕ1:

Я забыл добавить подпись ФУНКЦИИ

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:

Я понял это ...

Посмотреть решениегде я ответил на свой вопрос ....

1 Ответ

0 голосов
/ 02 апреля 2019

Вот решение:

Я изменил эту функцию:

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