Home > Software engineering >  how to get all data in correct way firebase-real-time-database JavaScript
how to get all data in correct way firebase-real-time-database JavaScript

Time:04-02

I am using node js and I am getting data from firebase real-time database here is no problem but the real problem is I am getting data something like this :

for data getting code! JS

import firebaseApp from '../config.js';
import { getDatabase, ref, onValue } from "firebase/database";


const userRef = ref(database, "Users");
onValue(userRef, (snapshot) => {

if (snapshot.exists) {
  const data = snapshot.val();
  console.log(data); // data printed to console
}

}, {
  onlyOnce: true
});

Console Output

{
  
 "random-user-id-1": {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "[email protected]"
 },
 
 "random-user-id-2": {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "[email protected]"
 },

 // and more...

}

And I can't able to show that info in lists as we can do in JSON

and want to show this data in a list or array how can I do that? example of expected output

[

    {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "[email protected]"
    },
    
    {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "[email protected]"
    }

    // and more........ ^_~

]
Any help will be always appreciated! and feel free to any doubt related to my question or problem!

thank you :)

CodePudding user response:

seems like you need only the values from your dict, you can transform your data this way:

const lst = {
  
 "random-user-id-1": {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "[email protected]"
 },
 
 "random-user-id-2": {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "[email protected]"
 },

}

const expectedFormatRes = Object.values(lst);


console.log(expectedFormatRes);

  • Related