0

我正在开发一个项目,在我的 component.ts 文件中,我正在调用一个 post API,它以格式给我一个响应

 email: string,
  employeeAddress: string,
  employeeId: string,
employeeLevel: string

我不确定我的方法是否正确,但这是 我的 API 调用代码

 return this.http.post<{[employeeId : string] : Create}>('http://localhost:9900/api/v1/employee',postData).
    pipe(map(response => {
      var res = JSON.parse(JSON.stringify(response))
        const getCreateempIdArray : any = [] ;
       
       console.log(res)
        for(const employeeId in res){
          if(res.hasOwnProperty(employeeId)){
            getCreateempIdArray.push({ ...res[employeeId]});
            }
           if(res.hasOwnProperty(email)){
             getCreateempIdArray .push({...res[email]});
           } 
        }
        console.log("Employee Id Array from Create employee Service " + getCreateempIdArray)
        return getCreateempIdArray;
      })).
   
  
     subscribe((responseData)=>
    {
       console.log(response)
      
      })

现在我想要的是,当我收到回复时,我想将我的employeeId 和与该employeeId 对应的电子邮件推送到一个数组中,以便我可以将数组保存在本地存储中,并且我可以通过employeeId 获取特定的员工电子邮件这个数组的帮助。

但是当我做 console.log(getCreateempIdArray) 它给了我 [object Object]

我只想将从响应正文中获取的employeeId及其相应的emailId推送到数组中

4

1 回答 1

0

它以 [object Object] 的形式显示,因为您正在将字符串与对象连接(使用 ' ' +)并且对象会自动转换为字符串。

试试这种方式:

console.log("Employee Id Array from Create employee Service ", getCreateempIdArray)

不要使用 (+),而是使用逗号 (,) 来分隔字符串和对象。

于 2022-02-07T20:49:28.150 回答