Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Firebase Realtime database query from v8 to v9 SDK

I have a Firebase Realtime database query written using Firebase version 8 that I'm looking to migrate over to the v9 SDK.

export const getSingleDataWithQuery = async ({ key, email, criteria }) => {
  if (!criteria) return;
  const snapshot = await realTimeDb
    .ref()
    .child(key)
    .orderByChild(query)
    .equalTo(criteria)
    .get();
  const val = snapshot.val();
  if (val) {
    const keys = Object.keys(val);
    return val[keys[0]];
  }
  return null;
};

In this example:

  • key would be the 'users' collection
  • the email field is looking for users by their email
  • and the criteria is the user's actual email (jane.doe@gmail.com)

Using Firebase's Read data once and Sorting data documentation I managed to narrow it down to perhaps being this, but I'm not sure if it's correct:

export const getSingleDataWithQuery = async ({ key, query, criteria }) => {
  if (!criteria) return;
  const dbRef = query(ref(realTimeDb, key), orderByChild(email), equalTo(criteria));
  get(dbRef).then(snapshot => {
    if (snapshot.exists()) {
      const val = snapshot.val();
      if (val) {
        const keys = Object.keys(val);
        return val[keys[0]];
      }
    }
  });
  return null;
};
about 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

Aside from the fact that you may have swapped query and email in the fragments, the only difference is in the way you handle the asynchronous database call and that likely explains the difference. Since you use then in the second snippet, the function doesn't actually return a promise and so calling it with await it in the caller won't have any effect.

To make those the same again, use await in the second snippet too, instead of then:

export const getSingleDataWithQuery = async ({ key, query, criteria }) => {
  if (!criteria) return;
  const dbRef = query(ref(realTimeDb, key), orderByChild(email), equalTo(criteria));
  const snapshot = await get(dbRef); // ๐Ÿ‘ˆ
  if (snapshot.exists()) {
    const val = snapshot.val();
    if (val) {
      const keys = Object.keys(val);
      return val[keys[0]];
    }
  }
  return null;
};
about 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!