How to establish the relation between two different trees in the root of the database using IDs?

只谈情不闲聊 提交于 2020-01-06 04:59:50

问题


I am trying to establish a relation between in my data structure between two different trees. One is the "Spaces" tree, the other is the "Depts". Logically the departments (depts) are inside the space (spaces).
This question comes from this one: How can I get to the references inside dynamically generated references in firebase?
Here is an image of my database structure:


回答1:


this seem to be the most practical method:

Tying the users node to the buildings:

Template:

    <p>Name: <input type="text" v-model="newBuilding.name"></p>
    <p>Address: <input type="text" v-model="newBuilding.address"></p>

Data:

data () {
    return {
      newBuilding: {
        name: '',
        address: '',
        ownerID: '',
      }
    }
  },

Methods:

addBuilding: function () {

    // get current user from firebase
      let userId = firebase.auth().currentUser.uid; 

    // create a random pushkey but without pushing it yet
      let buildingKey = buildingsRef.push().key 

    // set the user.uid as building data child "ownerId" value, (the other data nodes where hydrated from the <template>
      this.newBuilding.ownerID = userId; 

    // SET THE NEW BUILDING 
      buildingsRef.child(buildingKey).set(this.newBuilding); 

      // SET THE BUILDING PUCHKEY ON A CHILD NODE ON THE **USERS** NODE
  usersRef.child(userId).child('isAdmin').child(buildingKey).set(true);

} 

I hope this does it for you.

Follow more questions on this topic here:

What is the best practice to write the rules of Firebase in a situation like this?

Is it possible to define a variable on the firebase database references path?



来源:https://stackoverflow.com/questions/48523733/how-to-establish-the-relation-between-two-different-trees-in-the-root-of-the-dat

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