Calling a child inside 2 levels of nodes

后端 未结 1 864
借酒劲吻你
借酒劲吻你 2021-01-25 00:54

Trying to read the email of the current user(user1) who registered as a \"Guests\" in a certain hotel. so in my tableView, there will be a list of hotels that the current user e

相关标签:
1条回答
  • 2021-01-25 01:29

    Your current data structure makes it easy to find the guests for a given hotel. It does not however make it easy to find the hotels for a given guest.

    The reason for this is that Firebase Database queries work on a list-model. So you can for example order/filter hotels by their name or address, but not by their guests. So in your current data model you'll need to read all data of all hotels in order to then determine the list of hotels for a user.

    To allow querying the user for their hotels, add an inverted data structure:

    userHotels: {
      uid1: {
        hotelId1: true,
        hotelId2: true
      },
      uid2: {
        hotelId2: true,
        hotelId3: true
      }
    }
    

    With this additional data structure it becomes trivial to load the hotels for a user.

    Instead of true, you can also store some information about the hotel, so that you don't have to join that in your client-side code.

    Also see:

    • Firebase Query Double Nested
    • Firebase query if child of child contains a value
    • the Firebase documentation on keeping data flat
    0 讨论(0)
提交回复
热议问题