How to get firebase.database.Reference full path

consider

var adaRef = firebase.database().ref("users/ada");

How can I get the full path to ref? that is, "users / ada"?

+4
source share
2 answers

This is surprisingly simple:

adaRef.toString()

Prints the full URL: https://<your-app>firebaseio.com/users/ada

So, just to get the path, you substitute it there. Two ways to do this:

adaRef.toString().substring(firebase.database().ref().toString().length-1)

or

adaRef.toString().substring(adaRef.root.toString().length-1)

both will print /users/ada

+6
source

Following Frank's answer, I expanded firebase as follows:

firebase.database.Reference.prototype.fullkey = function() {
    return this.toString().substring(this.root.toString().length-1);
}

then you can do  adaRef.fullkey() // returns /users/ada

(I would like to call this "path", but it seems to be reserved by FB)

0
source

All Articles