Rakesh
Rakesh

Reputation: 4262

Handling Firebase Range Queries Response

Hi I am trying to handle response from firebase rest service as given in https://firebase.google.com/docs/database/rest/retrieve-data and following is a sample response which I receive:

{
  "2": {
    "name": "John",
    "surname": "Cena"
  },
  "12": {
    "name": "Murphy",
    "surname": "R ichard"
  },
  .
  .
  .
  "8": {
    "name": "Alisha",
    "surname": "Johnson"
  }
}

So the response is a random key and employee interface:

interface Employee{
  private name:string;
  private surname:string;
}

The number of elements in the response will differ. I want to handle this in typescript. I receive it back as Object and it not even array of map. I want to get array of Employees.Please advise the way to handle this.

Upvotes: 0

Views: 36

Answers (1)

rijin
rijin

Reputation: 1757

I would say its all based on what you are going to do with the data

// you can assign the response to employees
const employees: any = res;

// or you can convert this to array;
const data = Object.keys(res).map((key) => {
  return { ...res[key], id: key }; <-- here this can be pass to a constructor to return a Employee Object, also you can add a key/id field to the entity.
});

// or many more methods, its all based on how you are going access the data.

Upvotes: 1

Related Questions