Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // An issue you may run into when dealing with fetch calls to your firebase realtime database is that the data is likely not in the structure you want it. The data will come back in the form of an object with each item's ID is the key.
  2.  
  3. // Example response
  4.  
  5. {
  6. "-lk38hfnb": {
  7. title: "My Title 1",
  8. description: "My Description 1"
  9. },
  10. "-fn947bfh": {
  11. title: "My Title 2",
  12. description: "My Description 2"
  13. }
  14. }
  15.  
  16. // But you probably want your data to be in an array and look more like this:
  17. [
  18. {
  19. id: "-lk38hfnb",
  20. title: "My Title 1",
  21. description: "My Description 1"
  22. },
  23. {
  24. id: "-fn947bfh",
  25. title: "My Title 2",
  26. description: "My Description 2"
  27. }
  28. ];
  29.  
  30.  
  31. // Here is a utility function to map the response object to an array
  32.  
  33. const mapFBData = (data) => {
  34. return Object.keys(data)
  35. .map(key => {
  36. return { id: key, ...data[key] }
  37. });
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement