how to get firebase.database.Reference full path

后端 未结 3 2031
感动是毒
感动是毒 2021-01-17 22:54

consider

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

How can I get the full path of ref ? that is \"users/ada\" ?

相关标签:
3条回答
  • 2021-01-17 23:13

    That's surprisingly simple:

    adaRef.toString()
    

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

    So to just get the path, you substring it out of there. Two ways of doing that are:

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

    or:

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

    both will print /users/ada

    0 讨论(0)
  • 2021-01-17 23:17

    On new versions of the framework you could use:

    snapshot.ref.path.toString()

    being snapshot the result of a call on a Database Reference.

    Maybe this can be helpful.

    0 讨论(0)
  • 2021-01-17 23:17

    Following on Frank's answer I extended 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 that seems to be reserved by FB)

    0 讨论(0)
提交回复
热议问题