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

333
Views
Checking Firebase database for a key without downloading parent node?

I have a Firebase realtime database that's structured like this:

referrals
 Test1: 0
 Test2: 0

I'm trying to check if "referral_code" exists in the database, this is the code I'm using:

exports.validateReferral = functions.https.onCall((data, context) => {
    const referral_code = data.ref
    console.log("Cloud verifying referral code, code is " + referral_code)
    admin.database().ref().child("referrals").once("value", snapshot => {
        if (snapshot.hasChild(referral_code)) {
            console.log("referral code is valid");
            return true
        } else {
            console.log("referral code is not valid");
            return false
        }
    })
})

When I run it, it shows "Cloud verifying referral code, code is Test1" but it also returns not valid despite the key being in the database.

Any idea how I can debug this? I tried logging the value of "snapshot" but none of the contents of Firebase snapshots show the actual values. Can anyone help me figure out how to locate the problem?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

You're missing a return at the top level of your code:

// ๐Ÿ‘‡
return admin.database().ref().child("referrals").once("value").then((snapshot) => {
    if (snapshot.hasChild(referral_code)) {
        console.log("referral code is valid");
        return true
    } else {
        console.log("referral code is not valid");
        return false
    }
})

Without the top-level return, the return value from the asynchronous callback never reaches your client, and (worse) the code probably gets terminated before the result is ever read from the database.


But I recommend also changing the code to not download the entire referrals node just to check if one key exists. This accomplish the same and only downloads (at most) a single child node:

// ๐Ÿ‘‡                                                ๐Ÿ‘‡
return admin.database().ref().child("referrals").child(referral_code).once("value").then((snapshot) => {
    //   ๐Ÿ‘‡ 
    if (snapshot.exists()) {
        console.log("referral code is valid");
        return true
    } else {
        console.log("referral code is not valid");
        return false
    }
})
about 4 years ago ยท Juan Pablo Isaza 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!