Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the key from a Firebase data snapshot?

I'm able to query my users array with an e-mail address and return the user's account info:

users.orderByChild('email').equalTo(authData.user.email).once('value').then(function(snapshot) {         console.log(snapshot.val());         console.log(snapshot.key); // 'users'         console.log(snapshot.child('email').key); 'email'         ... 

enter image description here

How do I get the key (-KiBBDaj4fBDRmSS3j0r). snapshot.key returns users. snapshot.child('email').key returns email. The key doesn't appear to be a child, i.e., it appears to be in between users and email.

like image 269
Thomas David Kehoe Avatar asked Apr 25 '17 15:04

Thomas David Kehoe


People also ask

How do I get keys from Firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

How do I find my Firebase secret key?

In the Firebase console, open Settings > Service Accounts. Click Generate New Private Key, then confirm by clicking Generate Key.

Is there primary key in Firebase?

This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.

What is key and value in Firebase database?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

How do I retrieve specific data from a Firebase Database?

With Firebase database queries, you can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild(), orderByKey(), or orderByValue().

How to use firebase with Java?

Note that select Java as the programming language. After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.

How to navigate to Firebase Realtime Database in WordPress?

Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.

What is an asynchronous listener in Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.


1 Answers

You could do something like this:

var key = Object.keys(snapshot.val())[0]; 

Ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

like image 162
camden_kid Avatar answered Oct 20 '22 09:10

camden_kid