admin管理员组

文章数量:1291043

How to get the Firebase path (location string without the hostname part such as "users/user1") given the Firebase reference? I can't find in the docs and tried to search but no luck.

Now I did like this in Node.js, given Firebase reference ref:

var url = require('url'); // standard Node.js 'url' module.
var location = url.parse(ref.toString()).pathname;

Is this the correct way to do or there exist a more reliable way?

PS. I saw ref.path.o is the array containing the strings that can construct the path but I think it's not on the public API and may subject to change?

How to get the Firebase path (location string without the hostname part such as "users/user1") given the Firebase reference? I can't find in the docs and tried to search but no luck.

Now I did like this in Node.js, given Firebase reference ref:

var url = require('url'); // standard Node.js 'url' module.
var location = url.parse(ref.toString()).pathname;

Is this the correct way to do or there exist a more reliable way?

PS. I saw ref.path.o is the array containing the strings that can construct the path but I think it's not on the public API and may subject to change?

Share Improve this question edited Mar 22, 2016 at 18:02 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Mar 22, 2016 at 17:39 Saran S.Saran S. 2674 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

If you think of this outside of the context of Firebase, it bees "How do I get just the path from a URL?" Searching for that is bound to give good results. They'll likely be similar to what you came up with now.

Another approach might be to depend on Firebase 2.x SDK's root() method, which later became the root property in Firebase 3 SDK onwards to determine the base URL.

// For Firebase 2.x SDK
var location = ref.toString().substring(ref.root().toString().length);

// For Firebase 3 SDK and later
var location = ref.toString().substring(ref.root.toString().length);

本文标签: javascriptGet path of the Firebase referenceStack Overflow