简体   繁体   中英

Convert Firebase Query Result

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"
     
                          }
        }

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());
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM