Firebase gets full path to link URL in dataSnapshot (javascript API)

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 would like to specify a single handler, and then do all the processing in one place in my code, instead of defining the inline functions using 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 that dataSnapshot.name() returns only "reference" in both cases, which makes it impossible to distinguish between the two links in the switch / case statement!

I am sure that dataSnapshot contains this information somewhere, but I still need to disclose it in any convenient way. Examining the dataSnapshot object in the console, I found that there is an object embedded inside the called path that contains (among other things) an array using the example above that will contain ["firebase", "url", "A", "reference"] , but there is no easy way to access it.

If I had access to this array, I could rebuild the URL or find a more convenient way to handle the switch / case statement. I think a complete link string would be more suitable as an easily accessible value from dataSnapshot .

+7
source share
2 answers

To return from Snapshot to the full URL, you must:

 snapshot.ref().toString() 

The toString part is somewhat illogical. I often have to experience this to see if this is really the way to go.

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

UPDATE:

In recent versions of the SDK, ref no longer a function , so you have to use:

 snapshot.ref.toString(); 
+18
source

I need to add decodeURI function:

  decodeURI(snapshot.ref.toString()) 

Latin letters with accent, tilde, etc. (or symbol C).

0
source

Source: https://habr.com/ru/post/974758/


All Articles