如何让我的用户学生检索并显示他/她所属的教室?

时间:2018-09-28 12:14:09

标签: javascript firebase web firebase-realtime-database

我很难检索和显示他/她所属的学生用户的班级。在我的网络应用中,有两种类型的用户:教师学生

  

老师
  -可以创建和查看教室

     

学生
  -可以加入并查看教室

这些代码允许用户教师创建和显示教室:

  if(userRole.innerHTML == "Teacher"){
      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>';
                          })
                       })
                     }
                    }

这些代码允许用户学生加入特定的教室,但不会显示在html中。

function addclass(){

  var addclassID = document.getElementById("classroomID").value;
  var teacherUID =document.getElementById("teachID").value;
  var studentUID = user.uid;
  var studentName = user.displayName;
  var classRef = firebase.database()
                .ref().child('Classes').child(teacherUID);
  classRef.orderByChild("ClassID").equalTo(addclassID)
  .once("child_added", function(snapshot) {

      var studentsRef = snapshot.ref.child("MyStudents");
      studentsRef.child(studentUID).set({ Studentname: studentName });
    })

我试图使用这些代码来允许用户学生查看/显示他/她加入的课程:

else if(homeRole.innerHTML == "Student"){

var studentClassRef = firebase.database().ref()
                      .child('Classes' + '/' + user.uid);

    studentClassRef.on('child_added', function(data){

      var roomNamess = data.val().TheClass;
      var Studentxx = data.val().MyStudents;
      var classDD = data.val().ClassID;

      var ul2 = document.createElement('ul');
      document.getElementById('myLista').appendChild(ul2);
      var li2 = document.createElement('li');
      ul2.appendChild(li2);


      var studentRawLists= '';
      for (var keys in Studentxx) {
        if(studentxx[keys].Studentname == user.displayName){
          studentRawLists += ('['+Studentxx[keys].Studentname + ']');
      }    
          Object.keys(roomNamess).forEach(function(keyz){
            li2.innerHTML += '<span onclick="clickDone(this)">'
                             +roomNamess[keyz]+'</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>';

        })


    }
  })
  

我认为如果我使用此行

    for (var keys in Studentxx) {
        if(studentxx[keys].Studentname == user.displayName){
          studentRawLists += ('['+Studentxx[keys].Studentname + ']');
      }  
  

与用户学生名称匹配的名称将存储在 studentRawLists

出了什么问题?是否有其他替代方法可以检索和显示学生的教室?

和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"
      }
     }
    }
   }

因此,如果我使用用户甜通心粉,则应在HTML中显示2个教室,即:Classroom Trials asdasd 为用户紫色沙拉

0 个答案:

没有答案