Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 关于不兼容类型的Typescript编译器错误_Node.js_Typescript_Compiler Errors - Fatal编程技术网

Node.js 关于不兼容类型的Typescript编译器错误

Node.js 关于不兼容类型的Typescript编译器错误,node.js,typescript,compiler-errors,Node.js,Typescript,Compiler Errors,我正在开发一个API,它是用Typescript 3.9.7编写的,运行在节点10上。我已经删除了不必要的细节,但我基本上执行了以下操作: 从数据库中提取用户数据 向每个用户对象添加“状态”字段 将数据发送到UI 我试图使用接口来添加一些类型安全性,但我似乎误用了它们,因为TS编译器给了我一些错误。关于如何解决这一问题的建议将很有帮助 我删去了其他细节,但我的方法是获取用户数据并添加状态字段: public async getUsers( parameter: string

我正在开发一个API,它是用Typescript 3.9.7编写的,运行在节点10上。我已经删除了不必要的细节,但我基本上执行了以下操作:

  • 从数据库中提取用户数据
  • 向每个用户对象添加“状态”字段
  • 将数据发送到UI
  • 我试图使用接口来添加一些类型安全性,但我似乎误用了它们,因为TS编译器给了我一些错误。关于如何解决这一问题的建议将很有帮助

    我删去了其他细节,但我的方法是获取用户数据并添加状态字段:

    public async getUsers(
            parameter: string
        ): Promise<AugmentedUser[]> { 
        //return an array of User objects based on some parameter
        const userData = await this.userService.getAll<User>(parameter); 
        
        
        return userData.forEach((userRow: User) => {
               userRow.state = "inactive";
        });
    }
    
    我从编译器中得到以下错误:

    error TS2322: Type 'UserData[]' is not assignable to type 'AugmentedUser[]'. Property 'state' is missing in type 'User' but required in type 'AugmentedUser'.
    

    我做错了什么?我在
    forEach
    循环中添加了
    state
    字段,为什么会出现此错误?谢谢。

    forEach不退还任何东西。尝试映射功能:

    return userData.map((userRow: User) => {
      return {...userRow, state: 'inactive'};
    });
    
    这将生成一个包含所有用户属性以及AugmentedUser中存在的状态的对象列表

    return userData.map((userRow: User) => {
      return {...userRow, state: 'inactive'};
    });