我一直在网上寻找解决方案,可能是一个小问题。
我的功能还不错。文件:
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[]'
当我从以下位置更改:any时:
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:
我知道了...
在我回答自己问题的地方查看解决方案。...
答案 0 :(得分: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 ;
}