问题
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