如何使用ID在数据库的根目录中建立两个不同树之间的关系?

时间:2018-01-30 14:25:55

标签: firebase vue.js ref

我正在尝试在两个不同树之间的数据结构中建立关系。一个是" Spaces"树,另一个是" Depts"。从逻辑上讲,部门(部门)位于空间(空间)内 这个问题来自这个问题:How can I get to the references inside dynamically generated references in firebase?
这是我的数据库结构的图像:

Data structure

1 个答案:

答案 0 :(得分:0)

这似乎是最实用的方法:

用户节点绑定到建筑物

模板:

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

数据:

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

方法:

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);

} 

我希望这能为你做到。

在此处关于此主题的更多问题:

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?