Firebase get full path of reference URL in dataSnapshot (javascript API)

12,882

To get back from a Snapshot to the full URL, you do:

snapshot.ref().toString()

The toString part is somewhat counter-intuitive. I often find myself having to test it, to see if that's indeed the way to go.

hint It would be nice if there was also a more explicit getUrl method hint

UPDATE:

With recent SDK versions ref is no longer a function, so you'll have to use:

snapshot.ref.toString();
Share:
12,882
MandM
Author by

MandM

Updated on June 19, 2022

Comments

  • MandM
    MandM about 2 years

    Say I have the following:

    var firebaseARef = new Firebase("http://this.is.my/firebase/url/A/reference")
    var firebaseBRef = new Firebase("http://this.is.my/firebase/url/B/reference")
    

    When I define my .on() functions, I'd like to specify a single handler, and then do all of the handling in one place in my code, rather than having to define the functions inline with the .on() definition. To illustrate:

    var handleAllFirebaseStuff = function(dataSnapshot){
        var name = dataSnapshot.name(); //PROBLEM HERE: returns "reference", no way to distinguish!
        switch(name){
           case "http://this.is.my/firebase/url/A/reference": //How do I get this full reference from dataSnapshot?
              /* do stuff for A reference */
           case "http://this.is.my/firebase/url/B/reference": //How do I get this full reference from dataSnapshot?
              /* do stuff for B reference */
           default:
              break;
        }
    }
    
    firebaseARef.on('value', handleAllFirebaseStuff);
    firebaseBRef.on('value', handleAllFirebaseStuff);
    

    The problem is dataSnapshot.name() will only return "reference" in both cases, making it impossible to distinguish between the two references in the switch/case statement!

    I'm certain that dataSnapshot contains this information somewhere, but I have yet to uncover it in any convenient fashion. Exploring the dataSnapshotobject in the console, I find that there is an object buried within called path that contains (among other things) an array, using the example above, that would contain ["firebase", "url", "A", "reference"], but there is no easy way to access it.

    If I had access to that array, I could rebuild the URL or find a more convenient way to handle the switch/case statement. I think a full string of the reference would be more appropriate as an easily accessible value from dataSnapshot.

  • MandM
    MandM almost 10 years
    Yes! The API documentation for DataSnapshot.ref() in it's entirety is, "The Firebase reference for the location that generated this DataSnapshot." There is obviously more buried in here (i.e. .ref().toString()) -- let's see some more documentation! (or more helpful explicit methods hint hint)
  • MandM
    MandM almost 10 years
    Also, I'm glad that toString() works, but it being an undocumented method slightly scares me -- what if the method is totally changed in functionality between Firebase revs? (There is no guarantee it wont!) So, I agree with your statement about explicit method wholeheartedly - Firebase oughta move on this.
  • Frank van Puffelen
    Frank van Puffelen almost 10 years
    The fact that ref.toString() returns the full URL is documented: firebase.com/docs/web/api/firebase/tostring.html "Gets the absolute URL corresponding to this Firebase reference's location."
  • Matt Jensen
    Matt Jensen about 5 years
    If you want the relative path use: snapshot.ref.path.toString();