可能返回嵌套的可观测值:_scalar:false;明确返回未定义的对象



我遇到了一个问题,当我应该返回整个对象时,我却为对象中的一个值返回_scalar。我的对象中的这一个属性正在停止我的代码。所有参数都返回正确的值。

我正在努力记录尽可能多的代码,以帮助理解这个问题。我是最初级的,可以和我一起裸体。

相关视图模型(类应该是接口,我稍后会更改(:

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;

这是代码中断的地方:

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

这是我从控制台返回的内容。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

我必须将this.standardWaiver作为一个属性返回,该属性的值在参数中包含(this.waiverTypeId, this.policyNumber, this.dateOfLoss)。到目前为止,它还没有给我[object Object],我不明白为什么。我欢迎任何建议,并提前感谢您。

基本上searchForWaiver方法(包含observable(正在进行ajax调用。这意味着您必须使用.subscribesearchForWaiver方法,以便进行ajax调用。然后可以在.subscribe成功回调中获取searchForWaiver方法返回的数据。

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方法应该从所有可能的位置返回Observable。否则typescript将不允许您对该方法进行订阅。

因此更改

return null

return of(null)

最新更新