最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase getting only keys then getting data - Stack Overflow

programmeradmin4浏览0评论

i have a data structure as following at the url www.example.firebase/

{
  "panyList" : {
        "pkey1" : {
        "url1":"somelink1",
        "url2":somelink2
         },
         "pkey2" : {
         "url1":"somelink1",
         "url2":"somelink2"
         }
   }
}

What i want to achieve is that i want firebase to return first the list of panies which is

  pkey1
  pkey2

and not any child data

then if the user want to see a specific pany i want them to go to that url like so

  www.example.firebase/panyList/pkey2

new to firebase so explain as such.

i have a data structure as following at the url www.example.firebase./

{
  "panyList" : {
        "pkey1" : {
        "url1":"somelink1",
        "url2":somelink2
         },
         "pkey2" : {
         "url1":"somelink1",
         "url2":"somelink2"
         }
   }
}

What i want to achieve is that i want firebase to return first the list of panies which is

  pkey1
  pkey2

and not any child data

then if the user want to see a specific pany i want them to go to that url like so

  www.example.firebase./panyList/pkey2

new to firebase so explain as such.

Share Improve this question asked Jul 26, 2015 at 14:12 krvkrv 2,9408 gold badges45 silver badges83 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The Firebase JavaScript client always retrieves plete nodes. It has no option to retrieve only the keys.

If you want to retrieve only the keys/names of the pany, you'll have to store them in a separate node.

{
  "panyList" : {
     "pkey1" : {
        "url1":"somelink1",
        "url2":"somelink2"
     },
      "pkey2" : {
         "url1":"somelink1",
         "url2":"somelink2"
     }
  },
  "panyKeys" : {
     "pkey1": true,
     "pkey2": true
  }
}

A mon remendation in Firebase (and many other NoSQL databases) is to model your data in a way that your application will need to read it. In the above example, it seems like you need to read a list of pany keys, so that is what you should model.

Note: the Firebase REST API does have a shallow=true parameter that will return only the keys. But I remend solving the problem by modeling the data differently instead.

Firebase has a shallow parameter which can retrieve only the keys. I verified it's way faster (by a factor of 100), than retrieving the whole nodes.

Here it is in Google App Script (sorry):

class FirebaseNamespace {

  get database() {
    if(!this._database) {
      var firebaseUrl = "https://mydatabase.firebaseio./";
      var secret = "mysecret";
      this._database = FirebaseApp.getDatabaseByUrl(firebaseUrl, secret);
    }
    return this._database;
  }

  get(path, parameters) {
    return this.database.getData(path, parameters);
  }

  keys(path) {
    return Object.keys(this.get(path, {shallow:true}));
  }

  save(path, value) {
    this.database.setData(path, value);
    return value;
  }

}
发布评论

评论列表(0)

  1. 暂无评论