Angularfire: how to query the values of a specific key in an array?

后端 未结 1 870
闹比i
闹比i 2021-01-24 03:45

I have the following json object:

{
  \"posts\" : {
    \"-Jk3ipZ2EFgvkABHf9IX\" : {
      \"category\" : \"JavaScript\",
      \"date\" : 1426008529445,
      \         


        
相关标签:
1条回答
  • 2021-01-24 04:07

    Firebase's JavaScript SDK (on top of which AngularFire is built) will always load complete nodes. So there is no way to load just the category from your posts.

    The common way around this is to create a second top-level node that contains just the categories; or alternatively the categories and (for each) the push-id of each post in that category.

    {
      "posts" : {
        "-Jk3ipZ2EFgvkABHf9IX" : {
          "category" : "JavaScript",
          "date" : 1426008529445,
          "heading" : "Lorem",
          "text" : "dsvgfjds daefsrgfs defsds dsfsdds",
          "user" : "Mitko"
        },
        "-Jk3jFbOOE9V8Yd37tWj" : {
          "category" : "C#",
          "date" : 1426008640253,
          "heading" : "Lorem",
          "text" : "Some Text!",
          "user" : "Peter"
        }
      },
      "categories" : {
        "JavaScript" : {
          "-Jk3ipZ2EFgvkABHf9IX"
        },
        "C#" : {
          "-Jk3jFbOOE9V8Yd37tWj"
        }
    }
    

    It is a bit redundant, but allows you to get exactly the data you need to show a specific screen in your UI. Denormalizing your data like this is a common part of transitioning to a NoSQL database.

    0 讨论(0)
提交回复
热议问题