【问题标题】:Property 'emailAddress' does not exist on type 'BaseModel'.ts类型“BaseModel”.ts 上不存在属性“emailAddress”
【发布时间】: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&lt;PagedModel&gt;

标签: javascript angular typescript


【解决方案1】:

我不确定您的任务行为,但我认为您想从 API 响应中填写下拉列表,对吗?

为此,您创建了此模型 UserProfileDropdownDto 并且您希望将此模型填充为来自 API 响应的数组,之后您可以在 html 中使用它,

记住这个scenarios,下面的解决方案可能会对你有所帮助

请更新您的UserProfileDropdownDto 模型,如下所示

export class UserProfileDropdownDto extends BaseModel {
    id: number;
    fullName: string;
    roleDisplay: string;
    firstName:string;
    lastName:string;
    isChecked: boolean = false;
    emailAddress: string;
    ....
}

只要确保您的 UserProfileDropdownDto 模型,必须具有 API 响应的属性。

现在像这样在您的PagedModel 模型中进行更正,

export class PagedModel {
  firstItemOnPage: boolean;
  lastItemOnPage: boolean;
  totalItemCount: number;
  items: UserProfileDropdownDto[];

constructor(
  isFirstPage: boolean, 
  isLastPage: boolean, 
  totalItems: number, 
  itemList: UserProfileDropdownDto[]
) {
    this.firstItemOnPage = isFirstPage;
    this.lastItemOnPage = isLastPage;
    this.totalItemCount = totalItems;
    this.items = itemList;
}

【讨论】:

  • 不客气!
  • selectedNewUser 变量的类型是什么?我猜它的类型是BaseModel
  • 好吧,把@Input() selectedNewUser : Array&lt;Object&gt; = [];改成@Input() selectedNewUser : Array&lt;any&gt; = [];
  • 谢谢,你好@Suneel,也许你对这个问题有想法stackoverflow.com/questions/68709106/…谢谢,
  • 我查过,也回答了你的问题,请看一下
【解决方案2】:

根据您的评论!我已经写下了另一个问题的解决方案

当前结果

let data = {
  "id": 0,
  "name": "adas",
  "description": "dasdas",
  "status": "",
  "teamMembersDto": [
      {
        "id": 120098,
        "fullName": "xsc@gmail.com xsc@gmail.com",
        "roleDisplay": null,
        "firstName": "xsc@gmail.com",
        "lastName": "xsc@gmail.com",
        "emailAddress": "xsc@gmail.com",
        "phoneNumber": "12",
        "companyName": "xsc@gmail.com",
        "title": "xsc@gmail.com",
        "lastLogin": null,
        "createdDate": "09/08/2021 7:44:56 am",
        "isVerified": false,
        "roleDto": null,
        "status": "Active",
        "securityRole": "Unlicensed User",
        "lastLoggedIn": "",
        "teamCount": 0,
        "transactionRoleList": null
      },
      {
        "id": 40091,
        "fullName": "yukre@gmail.com yukre@gmail.com",
        "roleDisplay": null,
        "firstName": "yukre@gmail.com",
        "lastName": "yukre@gmail.com",
        "emailAddress": "yukre@gmail.com",
        "phoneNumber": "232423",
        "companyName": "yukre@gmail.com",
        "title": "3434",
        "lastLogin": null,
        "createdDate": "06/08/2021 12:35:32 pm",
        "isVerified": false,
        "roleDto": null,
        "status": "Active",
        "securityRole": "Unlicensed User",
        "lastLoggedIn": "",
        "teamCount": 0,
        "transactionRoleList": null
      }
  ],
  "accountId": 4
}

预期结果

let data = {
  "id": 0,
  "name": "adas",
  "description": "dasdas",
  "status": "",
  "teamMembersDto": [
      {
        "id": 0,
        "fullName": "xsc@gmail.com xsc@gmail.com",
        "roleDisplay": null,
        "firstName": "xsc@gmail.com",
        "lastName": "xsc@gmail.com",
        "emailAddress": "xsc@gmail.com",
        "phoneNumber": "12",
        "companyName": "xsc@gmail.com",
        "title": "xsc@gmail.com",
        "lastLogin": null,
        "createdDate": "09/08/2021 7:44:56 am",
        "isVerified": false,
        "roleDto": null,
        "status": "Active",
        "securityRole": "Unlicensed User",
        "lastLoggedIn": "",
        "teamCount": 0,
        "transactionRoleList": null,
        "memberId": 120098
      },
      {
        "id": 0,
        "fullName": "yukre@gmail.com yukre@gmail.com",
        "roleDisplay": null,
        "firstName": "yukre@gmail.com",
        "lastName": "yukre@gmail.com",
        "emailAddress": "yukre@gmail.com",
        "phoneNumber": "232423",
        "companyName": "yukre@gmail.com",
        "title": "3434",
        "lastLogin": null,
        "createdDate": "06/08/2021 12:35:32 pm",
        "isVerified": false,
        "roleDto": null,
        "status": "Active",
        "securityRole": "Unlicensed User",
        "lastLoggedIn": "",
        "teamCount": 0,
        "transactionRoleList": null
        "memberId": 40091
      }
  ],
  "accountId": 4
}

解决方案

data.teamMembersDto = data.teamMembersDto.map((item) => {
  let id = item.id;
  return {
    ...item,
    id: 0,
    memberId: id
  }
});

【讨论】:

  • 感谢您的帮助
  • 你的答案是正确的先生,我错了对不起
  • 没问题!不客气,如果您需要任何帮助,请告诉我,谢谢
  • 嗨@Suneel,你对这个问题有想法吗? stackoverflow.com/questions/68753148/… 。谢谢。
  • 嗨,@JellynAng!好的,让我检查一下
猜你喜欢
  • 1970-01-01
  • 2021-01-11
  • 2021-01-10
  • 2022-11-30
  • 1970-01-01
  • 2021-02-08
  • 2019-04-24
  • 2021-06-03
  • 2019-08-11
相关资源
最近更新 更多