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

256
Views
Use Variables for ChildSnapshot - Keys in Firebase (JavaScript)

I would like to use a variable for the ChildSnapshot but it does not work. This is how it works.

firebase.database(CustomerDatabase).ref('Customer').once('value', function (snapshot) {
    snapshot.forEach(function (ChildSnapshot) {
        console.log(ChildSnapshot.val().Firstname);
    }

This is how I would like it to work. But that throws an error.

let child_var = "Firstname";
firebase.database(CustomerDatabase).ref('Customer').once('value', function (snapshot) {
    snapshot.forEach(function (ChildSnapshot) {
        console.log(ChildSnapshot.val().child_var);
    }

Help appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You'll want to use [] notation for that:

console.log(ChildSnapshot.val()[child_var]);
about 4 years ago · Juan Pablo Isaza Report

0

This is a common JavaScript problem. What you are actually doing in the second code block is attempting to access the property named "child_var" instead of the one named "Firstname". Use bracketed property access to use a variable to access properties:

ChildSnapshot.val()[child_var]

Further reading: JavaScript property access: dot notation vs. brackets?

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!