【问题标题】:Type Error Angular : Undefined is not a function类型错误角度:未定义不是函数
【发布时间】:2022-01-21 03:45:27
【问题描述】:

selectedProjectInfo 是所选项目的数据,其中 ProjectAssignedUsers 然后,我从 API 获取 getemployeesList,然后使用字段 a 和修补数据收到此错误。

我想实现如果 Id,s 在两个列表中匹配并且我得到了名字然后在表单上提交了这个名字

if(this.selectedProjectInfo.ProjectAssignedUsers)
this.selectedProjectInfo.ProjectAssignedUsers.forEach(
  this.getemployeesList.forEach((cacheEmployee: any) => {
    (backendAssignUser: any) => {
      if (
        cacheEmployee?.Id === backendAssignUser?.UserId &&
        backendAssignUser.UserAssignmentType === 1
      ) {
        this.salesRepList.push({
          FirstName: cacheEmployee?.FirstName,
          LastName: cacheEmployee?.LastName,
          UserTradeNames: cacheEmployee?.UserTradeNames,
          Id: cacheEmployee?.Id,
          UserAssignmentType: 1,

          Color: cacheEmployee?.Color,
        });
      } else if (
        cacheEmployee.Id === backendAssignUser.UserId &&
        backendAssignUser.UserAssignmentType === 2
      ) {
        this.estimatorsList.push({
          FirstName: cacheEmployee?.FirstName,
          LastName: cacheEmployee?.LastName,
          UserTradeNames: cacheEmployee?.UserTradeNames,
          Id: cacheEmployee?.Id,
          UserAssignmentType: 2,
          Color: cacheEmployee?.Color,
        });
      } else if (
        cacheEmployee.Id === backendAssignUser.UserId &&
        backendAssignUser.UserAssignmentType === 3
      ) {
        this.pmList.push({
          FirstName: cacheEmployee.FirstName,
          LastName: cacheEmployee.LastName,
          UserTradeNames: cacheEmployee.UserTradeNames,
          Id: cacheEmployee.Id,
          UserAssignmentType: 3,
          Color: cacheEmployee.Color,
        });
      }
    }
  }),

【问题讨论】:

  • 意大利面条代码,不错!

标签: angular typescript typeerror angular12


【解决方案1】:

你搞砸了如何在数组上使用forEach 方法。

this.selectedProjectInfo.ProjectAssignedUsers.forEach(
  this.getemployeesList.forEach((cacheEmployee: any) => {

您可能希望在第二个 forEach 中进行 1 次嵌套迭代。在这种情况下,正确的方法如下

 this.selectedProjectInfo.ProjectAssignedUsers.forEach(
      (projectAssignedUser: any) => {
        this.getemployeesList.forEach((cacheEmployee: any) => {
              .....
              .....
      }

检查以下简单示例,并查看代码中产生的完全相同的错误

var array1 = [1, 2];
var array2 = [3, 4, 5];

array1.forEach(
    array2.forEach((array2Element) => {
             console.log(array2Element);
             }));

虽然以下是您想要并且可以工作的嵌套循环

var array1 = [1, 2];
var array2 = [3, 4, 5];

array1.forEach((array1Element)=> {
    array2.forEach((array2Element) => {
             console.log(array2Element);
             })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2019-02-05
    • 2019-07-18
    • 1970-01-01
    • 2015-03-14
    • 2014-08-11
    • 2015-04-14
    相关资源
    最近更新 更多