【发布时间】:2021-08-09 21:08:42
【问题描述】:
我有一个从服务调用 api 的功能,现在我想为 res.items 上的每个执行操作并检查 emailAddress 但我无法循环通过它并且我收到一个错误属性“emailAddress”不存在在类型“BaseModel”.ts 上,我不明白为什么。
导致这个问题的原因似乎是什么?为什么每个人都不起作用?有什么想法吗?
这是来自 _transactionUserPageEvent 的示例数据
#组件A服务
getUserProfileTableDropdown(
id: number,
page: number,
pageSize: number,
searchString: string,
sortKey: string[],
sortOrder: string[]
): Observable<PagedModel> {
const params = new HttpParams()
.set('id', id.toString())
.set('page', page.toString())
.set('pageSize', pageSize.toString())
.set('searchString', searchString)
.set('sortKey', JSON.stringify(sortKey) || '')
.set('sortOrder', JSON.stringify(sortOrder) || '');
return this.httpRequestService.get<PagedModel>(
`${apiBaseUrl}/table/profiles`,
params
);
}
#组件 A TS 代码
private _transactionUserPageEvent() {
this.isTransactionUserLoading = true;
this.transactionUserTable.data = [];
this._userProfileService.getUserProfileTableDropdown(
this.accountId,
this.transactionUserTable.pageIndex + 1,
this.transactionUserTable.pageSize,
this.searchTransactionUserInput.nativeElement.value,
this.transactionUserTable.sortParams,
this.transactionUserTable.sortDirs
)
.pipe(
finalize(() => this.isTransactionUserLoading = false)
)
.subscribe({
error: err => this._notificationService.showError(err),
next: res => {
console.log("new users" , this.selectedNewUser)
this.transactionUserTable.totalElements = res.totalItemCount;
this.transactionUserTable.data = res.items as UserProfileDropdownDto[];
this.totalData = res.totalItemCount;
this.currentDisplayedData = res.lastItemOnPage;
res.items.forEach(item => {
if(item.emailAddress && selectedNewUser.findIndex(x => x.emailAddress === item.emailAddress) !== -1){
item.checked = true;
}
});
},
complete: noop
});
}
#base-model.ts
export class BaseModel {
// createdStr: string;
// createdByUser: AuditUser;
// modifiedStr: string;
// modifiedByUser: AuditUser;
}
export class PagedModel {
firstItemOnPage: boolean;
lastItemOnPage: boolean;
totalItemCount: number;
items: BaseModel[];
constructor(isFirstPage: boolean, isLastPage: boolean, totalItems: number, itemList: BaseModel[]) {
this.firstItemOnPage = isFirstPage;
this.lastItemOnPage = isLastPage;
this.totalItemCount = totalItems;
this.items = itemList;
}
}
#dto 用户代码
export class UserProfileDropdownDto {
id: number;
fullName: string;
roleDisplay: string;
firstName:string;
lastName:string;
isChecked: boolean = false;
}
【问题讨论】:
-
selectedNewUser模型是否具有 emailAddress 属性?你能分享它的对象数据吗?, -
@SuneelKumar 是的,我认为问题出在 res.items 上,尽管我只会 res.items.forEach(item => { console.log("item.emailAddress" , item.emailAddress ) });它说属性'emailAddress'在类型'BaseModel'.ts(2339)上不存在
-
是的,我检查了你上面的
BaseModel,好像你里面没有emailAddress -
如果我将 emailAddress 添加到 BaseModel 这将是新的错误imgur.com/a/49VWHAo
-
好的!这是因为您在
getUserProfileTableDropdown()上应用的类型,因为它的响应类型应该是Observable<PagedModel>
标签: javascript angular typescript