Get path of the Firebase reference

走远了吗. 提交于 2019-12-23 07:31:13

问题


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?


回答1:


If you think of this outside of the context of Firebase, it becomes "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's built-in root() method to determine the base URL.

var location = ref.toString().substring(ref.root().toString().length);


来源:https://stackoverflow.com/questions/36161508/get-path-of-the-firebase-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!