问题
In my web app, there are 2 types of user: Teacher and Student
A Teacher can create virtual classrooms and view the students who joined his/her rooms.
While Student can join a classroom and should be able to see the classroom he/she joined to.
These codes allow the Teacher to create a classroom:
function classcreation(q)
{
var checkcn = document.getElementById('classroomName').value;
if(checkcn == "" && checkcn == null){
alert("Empty Class Name!!");
}
else {
var usuid = generateId();
var myClasses={};
myClasses.TheClass =
document.getElementById('classroomName').value;
myClasses.Teacher = user.displayName;
myClasses.TeacherID = user.uid;
myClasses.ClassID = usuid;
fbclass.child(user.uid).push().set(myClasses);
}
}
and these to allow the teacher to view the classrooms he/she created as well as the students who joined his/her room/s.
var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
userRef.on('child_added', function(data)
{
var roomNames = data.val().TheClass;
var Studentx = data.val().MyStudents;
var studentRawList = '';
for (var key in Studentx) {
studentRawList += ('['+Studentx[key].Studentname + ']');
}
var classD = data.val().ClassID;
var ul = document.createElement('ul');
document.getElementById('myList').appendChild(ul);
var li = document.createElement('li');
ul.appendChild(li);
Object.keys(roomNames).forEach(function(key){
li.innerHTML += '<span onclick="clickDone(this)">'
+roomNames[key]+'</span>
<ul style="display:none">
<li>Class Id : '+classD+'</li>
<li><span onclick="clickDone(this)">Students :
</span><ul style="display:none">
<li>'+studentRawList+'</li></ul></li></ul>';
});
});
JSON:
{
"Accounts" : {
"FykyhzEZjndylFj3BbCnPqoTGDo1" : {
"displayName" : "Dodong Advices",
"email" : "advicenidodong@gmail.com",
"status" : "Teacher"
},
"HOgdSlTed9V8g0kSZjizgODMDOe2" : {
"displayName" : "Sweet Macaroni",
"email" : "Sweet@gmail.com",
"status" : "Student"
},
"yJif4ReTxCcGmo682xWSG3L5MKE3" : {
"displayName" : "Purple Salad",
"email" : "Purple@gmail.com",
"status" : "Student"
}
},
"Classes" : {
"FykyhzEZjndylFj3BbCnPqoTGDo1" : {
"-LMpvlBl3mEazhxaJwqb" : {
"ClassID" : "6503-3503-6827",
"Teacher" : "Dodong Advices",
"TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
"TheClass" : "StackMates"
},
"-LMrfIBg8v-hj1k8X2Qf" : {
"ClassID" : "7583-2402-2757",
"MyStudents" : {
"HOgdSlTed9V8g0kSZjizgODMDOe2" : {
"Studentname" : "Sweet Macaroni"
}
},
"Teacher" : "Dodong Advices",
"TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
"TheClass" : "asdasd"
},
"-LMrfMV1aw3YNA0PfooR" : {
"ClassID" : "8083-2712-3347",
"MyStudents" : {
"HOgdSlTed9V8g0kSZjizgODMDOe2" : {
"Studentname" : "Sweet Macaroni"
},
"yJif4ReTxCcGmo682xWSG3L5MKE3" : {
"Studentname" : "Purple Salad"
}
},
"Teacher" : "Dodong Advices",
"TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
"TheClass" : "Trial"
}
}
}
}
So based on JSON, we have 2 Student user named: Sweet Macaroni and Purple Salad. Sweet Macaroni joined a class asdasd and Trial while Purple Salad only joined the classroom Trial
The question is, how do I allow the each user student to see/view the rooms they belong to?
Also tried these codes but didn't worked:
var studentRef = firebase.database().ref().child('Classes' + '/' +
user.uid);
studentRef.on('child_added', function(data)
{
var roomNamess = data.val().TheClass;
var Studentxx = data.val().MyStudents;
var studentRawLists = '';
for (var keys in Studentxx) {
if(studentxx[keys].Studentname == user.displayName){
studentRawLists +=
('['+Studentxx[keys].Studentname + ']');
}
}
var classDD = data.val().ClassID;
var ull = document.createElement('ul');
document.getElementById('myLista').appendChild(ull);
var lii = document.createElement('li');
ull.appendChild(lii);
Object.keys(roomNamess).forEach(function(key){
lii.innerHTML += '<span onclick="clickDone(this)">'
+roomNamess[key]+
'</span><ul style="display:none">
<li>Class Id : '+classDD+'</li>
<li><span onclick="clickDone(this)">
Students :</span>
<ul style="display:none">
<li>'+studentRawLists+'</li></ul>
</li></ul>';
});
});
回答1:
{
"Classes": {
"FykyhzEZjndylFj3BbCnPqoTGDo1": {
"students": [
"-LMpvlBl3mEazhxaJwqb",
"-LMrfIBg8v-hj1k8X2Qf",
"-LMrfMV1aw3YNA0PfooR"
],
"staff": [
"-LMpvlBsadsadsdadsad"
],
"details": {
"title": "Leaning English",
"room_number": "L32",
"time_start": "14:00",
"time_end": "17:00"
}
}
}
}
Its going to depend of how you query the data but I would guess that the user, be pupil or teacher will want to see their assigned rooms so you should scan each class for the users uid.
In NoSQL you should not be afraid of recording data more than once in different locations, to save on additional querying.
Updated Explanation of Json.
Let me literate the json for you.
for(i in this.data){
var uid = user.uid;
var student = this.data[i].val.students;
for(i2 in student){
if(student[i2] == uid){
//student assigned to room
console.log(this.data[i].val.details);
}
}
}
来源:https://stackoverflow.com/questions/52531192/how-do-i-make-my-user-student-view-all-the-classrooms-he-she-belong-to