Instantly share code, notes, and snippets.

Embed
What would you like to do?
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));
});
@guibot17

This comment has been minimized.

guibot17 commented Sep 15, 2015

hello

@guibot17

This comment has been minimized.

guibot17 commented Sep 15, 2015

Hey

@lokeshjain2008

This comment has been minimized.

lokeshjain2008 commented Mar 4, 2016

grt

@idanb11

This comment has been minimized.

idanb11 commented Apr 15, 2016

line 6, need to be updated to :

return ref.parent().key();

@kidequinox

This comment has been minimized.

kidequinox commented Jan 30, 2017

so very helpful when using child_changed, and targeting nested children and wanting to know their parent ID for example.

@kidequinox

This comment has been minimized.

kidequinox commented Feb 25, 2017

And you can do this:
ref.parent().parent().key()

@kyle-wendling

This comment has been minimized.

kyle-wendling commented Mar 18, 2017

var ref = snapshot.ref(); is invalid? This throws a TypeError?!
'ref' is a property not a function, see: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#ref

@rathodvisha

This comment has been minimized.

rathodvisha commented Mar 22, 2017

how to fetech data in database using node js without using child in firebase

@rimagahwa

This comment has been minimized.

rimagahwa commented Mar 31, 2017

Thank you !

@nanborklabs

This comment has been minimized.

nanborklabs commented Jun 27, 2017

For Database Trigger Operation , to get the Immediate parent , i used
var parentKey = snapshot.data.ref.parent.key;

@Sarasranglt

This comment has been minimized.

Sarasranglt commented Aug 28, 2017

This worked -> snapshot.ref.parent.key

@tehong

This comment has been minimized.

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

This comment has been minimized.

soyharso commented Sep 21, 2017

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

@jess010

This comment has been minimized.

jess010 commented Oct 4, 2017

Thumbs up for snapshot.ref.parent.key

@ytianhui

This comment has been minimized.

ytianhui commented Nov 7, 2017

Thanks a lot for snapshot.ref.parent.key

@murugesh1

This comment has been minimized.

murugesh1 commented Jan 19, 2018

hai

@murugesh1

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

BernardMarieOnzo commented Mar 29, 2018

hum

@pebojot

This comment has been minimized.

pebojot 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.

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