Skip to content

Instantly share code, notes, and snippets.

@anantn
Last active April 14, 2021 15:29
Show Gist options
  • Save anantn/4964365 to your computer and use it in GitHub Desktop.
Save anantn/4964365 to your computer and use it in GitHub Desktop.
Firebase: Get the parent of a snapshot.
function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref();
// Now simply find the parent and return the name.
return ref.parent().name();
}
var testRef = new Firebase("https://example.firebaseIO-demo.com/foo/bar");
testRef.once("value", function(snapshot) {
// Should alert "Name of the parent: foo".
alert("Name of the parent: " + getParent(snapshot));
});
@tehong
Copy link

tehong commented Sep 12, 2017

snapshot.ref.parent() is incorrect and throws a type error ('parent()' is not a function). snapshot.ref.patent works.

@soyharso
Copy link

Use childSnapshot.ref.parent.getKey() to obtain the parent key.

@jess010
Copy link

jess010 commented Oct 4, 2017

Thumbs up for snapshot.ref.parent.key

@mikeyang01
Copy link

mikeyang01 commented Nov 7, 2017

Thanks a lot for snapshot.ref.parent.key

@murugesh1
Copy link

hai

@murugesh1
Copy link

murugesh1 commented Jan 19, 2018

how to fix this issue ,i am also follow above code
firebase: snapshot.getKey is not a function jquery

@murugesh1
Copy link

murugesh1 commented Jan 19, 2018

But my shapshot file this

`function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref();
var key = Object.keys(snapshot.val())[4];

// Now simply find the parent and return the name.
return key;
}

var firebaseRef_ch = new Firebase("my firebaseio.com/");
firebaseRef_ch.once("value", function(snapshot) {
// Should alert "Name of the parent: foo".
//alert("Name of the parent: " + getParent(snapshot));

});
`

@BernardMarieOnzo
Copy link

hum

@pebojote
Copy link

pebojote commented Dec 4, 2018

Just use this:

var newPostKey = firebase.database().ref().child('posts').push().key;

After that, use the variable because it contains the key.

@hanadjerbi1
Copy link

how i could get all the key's name
any help?

@mark234qin
Copy link

You can get all the snapshot keys by
Object.keys(snapshot.val());

@lohwa
Copy link

lohwa commented Apr 5, 2019

Hi everyone i have a big problem about my function it has an error and this it says: Error: function crashed. Details: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.

And this is my code:

`const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNewEmergencyNotification = functions.database.ref('messages/{id}').onCreate(event=>{

console.log('User to send notification');

var ref = admin.database().ref(`users/allusers/${pushId}/token`);
return ref.once("value", function(snapshot){
     const payload = {
          notification: {
              title: 'An Emergency has been Reported!!',
              body: 'Tap here to view details..'
          }
     };

     admin.messaging().sendToDevice(snapshot.val(), payload)

}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});

})`

@lohwa
Copy link

lohwa commented Apr 5, 2019

thank you in advance

@RameshMathad
Copy link

how to retrieve parent using a collection of children firestore web app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment