'How to map and retrieve data at index 0 in Nest JS?

Here is my code:

export class DependantsService {

  constructor(            
    private sequelize: Sequelize
  ) { }

    async showDependants(EmpId: string): Promise<any> {
        var response: dtoDependants = new dtoDependants();
        var data = await this.sequelize.query('Exec API_Select_EmployeeDependants :EmployeeId',
          {
            replacements: { EmployeeId: EmpId},
            type: sequelize.QueryTypes.SELECT
          });
          if(data.length>0){

             data.map((item: any) => {  
              response.employeeId=item.EmployeeId;
              response.Id=item.Id;
              response.fullname=item.Fullname;
              response.relationship=item.Relationship;
              response.gender=item.Gender;
              response.maritalstatus=item.MaritalStatus;
              response.dob=item.DOB;
              response.nic=item.NIC;
    
             });
    
          return response;
          }

    }
}

Now there are 2 records in the SQL Server database and it is returning the last one. If I only do return data; It returns both the records in the database. I want to somehow map values at index 0,1, etc. and then return response; so that i can get any of the records i like from the database. I have tried using data[Object.keys(data)[0]] but it doesn't work and return a 500 error. Any help would be appreciated. Thankyou.



Solution 1:[1]

Try it like this:

if(data.length > 0) {
  return {  
    employeeId: data[0].EmployeeId,
    Id: data[0].Id,
    fullname: data[0].Fullname,
    relationship: data[0].Relationship,
    gender: data[0].Gender,
    maritalstatus: data[0].MaritalStatus,
    dob: data[0].DOB,
    nic: data[0].NIC    
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tobias S.