Home > Mobile >  Convert Firebase Query Result
Convert Firebase Query Result

Time:08-31

I'm trying to retrieve data from my Firebase Realtime Database using Javascript. Upon console-logging the result, I am stuck with what I presume is the query's metadata (pasted below).

Xs {_repo: Ts, _path: Me, _queryParams: jt, _orderByCalled: true}...

I wish to interact with this data within Javascript. How can I convert this result to a readable format or access its individual children and values?

Here's the code I'm using:

const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);

var test = query(ref(db, "/Donors"), orderByValue(), 
limitToFirst(3))

console.log(test)

(I have removed config details because security rules have not yet been enforced.)

Here's how the database looks:

Donors  {

            John Smith

                         {
                            firstName: "John",
                            lastName: "Smith"
     
                          },
            Jack Smith

                         {
                            firstName: "Jack",
                            lastName: "Smith"
     
                          },
            Jeff Smith

                         {
                            firstName: "Jeff",
                            lastName: "Smith"
     
                          },
            Josh Smith

                         {
                            firstName: "Josh",
                            lastName: "Smith"
     
                          }
        }

CodePudding user response:

You're logging the Query, for which the output looks correct.

If you want to get the actual results and log those, you'll need to execute the query first for example by listening to a value event on it or getting the data once.

Based on those links:

get(test).then((snapshot) => {
  snapshot.forEach((child) => {
    console.log(child.key, child.val());
  });
});
  • Related