可能返回一个嵌套的可观察对象:_scalar: false;明确地返回未定义的对象
Possibly returning a nested observable: _scalar: false; Definetly returning undefined objects
我遇到一个问题,当我应该 return 整个对象时,我正在 returning 对象中的值的 _scalar。我对象中的这个 属性 正在停止我的代码。参数都是return正确值
我正在尝试记录尽可能多的代码以帮助理解问题。我是最年轻的我可以这么赤裸。
相关视图模型(class应该是我稍后会更改的界面):
export class StandardWaiver {
id?: number;
waiverType?: WaiverType;
duplicateFound?: boolean;
notFoundInCAP?: boolean;
policyNumber?: string;
policyType?: number;
dateOfLoss?: Date;
豁免服务:
export class WaiverSevice {
privat baseURL = '/xxx/XXX/xxxxxx/';
constructor(private http: HttpClient) {}
searchForWaiver(waiverTypeId: number, policyNumber: string, dateOfLoss: Date): any {
let options = this.setOptionHeaders();
if (isNotNullOrUndefined(policyNumber) && isNotNullOrUndefined(dateOfLoss)) {
const duplicateCheck: any = {
policyNumber,
dateOfLoss,
};
if (waiverTypeId === 1) { return this.http.post(this.baseURL +
'Standard/CheckForDuplicateOrCAP', duplicateCheck, options); }
else if (waiverTypeId === 2) { return this.http.post(this.baseURL +
'ICC/CheckForDuplicateOrCAP', duplicateCheck, options); }
else {
return null;
}
}
}
export const standardWaiverFromJSON = (data: any) => {
let standardWaiver: StandardWaiver;
standardWaiver.id = isNotNullOrUndefined(data?.id) ? data.id : null;
this is where the code breaks:
export class CreateNewWaiverModalComponent {
public opened = false;
standardWaiver: StandardWaiver = new StandardWaiver();
dateOfLoss: Date = null;
policyNumber: string;
waiverTypeId: number;
waiverData: any;
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private waiverService: WaiverService) { }
async searchForDuplicateAndCAP() {
if (this.waiverTypeId === 1)// standard
{
this.waiverData = this.waiverService.searchForWaiver(this.waiverTypeId, this.policyNumber,
this.dateOfLoss);
console.log('DATA: ' + this.waiverData, '; WAIVERTYPEID: ' + this.waiverTypeId, ';
POLICYNUMBER: ' + this.policyNumber, '; DATEOFLOSS: ' + this.dateOfLoss, '; STANDARDWAIVER
OBJECT: ' + this.standardWaiver);
this.standardWaiver = standardWaiverFromJSON(this.waiverData);
}
这就是我 return 从 console.log:
DATA: [object Object] ; WAIVERTYPEID: 1 ; POLICYNUMBER: 1234556654 ; DATEOFLOSS: Thu Jan 31
2019 00:00:00 GMT-0500 (Eastern Standard Time) ; STANDARDWAIVER OBJECT: [object Object]
ERROR Error: Uncaught (in promise): TypeError: standardWaiver is undefined - firefox browser
ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'id' of undefined - chrome browser
我必须 return this.standardWaiver 作为 属性,参数中的值具有 (this.waiverTypeId, this.policyNumber, this.dateOfLoss)
。到目前为止,它并没有给我 [object Object]
并且我不明白为什么。我欢迎任何建议,并提前谢谢你。
基本上 searchForWaiver
方法(包含可观察的)进行 ajax 调用。这意味着你必须 .subscribe
到 searchForWaiver
方法,这样 ajax 调用才会发生。然后你可以从 .subscribe
成功回调中的 searchForWaiver
方法获取数据 returned。
async searchForDuplicateAndCAP() {
if (this.waiverTypeId === 1)// standard
{
this.waiverService.searchForWaiver(this.waiverTypeId, this.policyNumber,
this.dateOfLoss).subscribe(
data => {
this.waiverData = data;
console.log('DATA: ' + this.waiverData, '; WAIVERTYPEID: ' + this.waiverTypeId, ';
POLICYNUMBER: ' + this.policyNumber, '; DATEOFLOSS: ' + this.dateOfLoss, '; STANDARDWAIVER
OBJECT: ' + this.standardWaiver);
this.standardWaiver = standardWaiverFromJSON(this.waiverData);
});
}
再更正一次,searchForWaiver
方法应该 return Observable
来自所有可能的地方。否则打字稿将不允许您订阅该方法。
因此改变
return null
到
return of(null)
我遇到一个问题,当我应该 return 整个对象时,我正在 returning 对象中的值的 _scalar。我对象中的这个 属性 正在停止我的代码。参数都是return正确值
我正在尝试记录尽可能多的代码以帮助理解问题。我是最年轻的我可以这么赤裸。
相关视图模型(class应该是我稍后会更改的界面):
export class StandardWaiver {
id?: number;
waiverType?: WaiverType;
duplicateFound?: boolean;
notFoundInCAP?: boolean;
policyNumber?: string;
policyType?: number;
dateOfLoss?: Date;
豁免服务:
export class WaiverSevice {
privat baseURL = '/xxx/XXX/xxxxxx/';
constructor(private http: HttpClient) {}
searchForWaiver(waiverTypeId: number, policyNumber: string, dateOfLoss: Date): any {
let options = this.setOptionHeaders();
if (isNotNullOrUndefined(policyNumber) && isNotNullOrUndefined(dateOfLoss)) {
const duplicateCheck: any = {
policyNumber,
dateOfLoss,
};
if (waiverTypeId === 1) { return this.http.post(this.baseURL +
'Standard/CheckForDuplicateOrCAP', duplicateCheck, options); }
else if (waiverTypeId === 2) { return this.http.post(this.baseURL +
'ICC/CheckForDuplicateOrCAP', duplicateCheck, options); }
else {
return null;
}
}
}
export const standardWaiverFromJSON = (data: any) => {
let standardWaiver: StandardWaiver;
standardWaiver.id = isNotNullOrUndefined(data?.id) ? data.id : null;
this is where the code breaks:
export class CreateNewWaiverModalComponent {
public opened = false;
standardWaiver: StandardWaiver = new StandardWaiver();
dateOfLoss: Date = null;
policyNumber: string;
waiverTypeId: number;
waiverData: any;
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private waiverService: WaiverService) { }
async searchForDuplicateAndCAP() {
if (this.waiverTypeId === 1)// standard
{
this.waiverData = this.waiverService.searchForWaiver(this.waiverTypeId, this.policyNumber,
this.dateOfLoss);
console.log('DATA: ' + this.waiverData, '; WAIVERTYPEID: ' + this.waiverTypeId, ';
POLICYNUMBER: ' + this.policyNumber, '; DATEOFLOSS: ' + this.dateOfLoss, '; STANDARDWAIVER
OBJECT: ' + this.standardWaiver);
this.standardWaiver = standardWaiverFromJSON(this.waiverData);
}
这就是我 return 从 console.log:
DATA: [object Object] ; WAIVERTYPEID: 1 ; POLICYNUMBER: 1234556654 ; DATEOFLOSS: Thu Jan 31
2019 00:00:00 GMT-0500 (Eastern Standard Time) ; STANDARDWAIVER OBJECT: [object Object]
ERROR Error: Uncaught (in promise): TypeError: standardWaiver is undefined - firefox browser
ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'id' of undefined - chrome browser
我必须 return this.standardWaiver 作为 属性,参数中的值具有 (this.waiverTypeId, this.policyNumber, this.dateOfLoss)
。到目前为止,它并没有给我 [object Object]
并且我不明白为什么。我欢迎任何建议,并提前谢谢你。
基本上 searchForWaiver
方法(包含可观察的)进行 ajax 调用。这意味着你必须 .subscribe
到 searchForWaiver
方法,这样 ajax 调用才会发生。然后你可以从 .subscribe
成功回调中的 searchForWaiver
方法获取数据 returned。
async searchForDuplicateAndCAP() {
if (this.waiverTypeId === 1)// standard
{
this.waiverService.searchForWaiver(this.waiverTypeId, this.policyNumber,
this.dateOfLoss).subscribe(
data => {
this.waiverData = data;
console.log('DATA: ' + this.waiverData, '; WAIVERTYPEID: ' + this.waiverTypeId, ';
POLICYNUMBER: ' + this.policyNumber, '; DATEOFLOSS: ' + this.dateOfLoss, '; STANDARDWAIVER
OBJECT: ' + this.standardWaiver);
this.standardWaiver = standardWaiverFromJSON(this.waiverData);
});
}
再更正一次,searchForWaiver
方法应该 return Observable
来自所有可能的地方。否则打字稿将不允许您订阅该方法。
因此改变
return null
到
return of(null)