在我的网络应用中,有2种用户:教师和学生


  老师们:
  -可以创建具有唯一教室ID的教室
  -可以在他/她创建的每个教室中查看他/她的学生
  
  学生们
  -可以加入教师使用教室ID创建的教室
  -可以查看他/她加入的每个同学


对于老师,我使用以下代码创建教室:

function classcreation(q)
{
    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);
}


这些代码可以查看每个用户。教师创建了教室:

var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
userRef.on('child_added', function(data)
{
    var roomNames = data.val().TheClass;
    var Studentx = data.val().TheStudents;
    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>Students : '+Studentx+'</li>
            </ul>';
    });
});


我试图将这些代码用于user.students加入课堂

function addclass() {
    var addclassID = document.getElementById("classroomID").value;
    var checkclassID = firebase.database().ref().child('Classes' + '/' +
    user.uid);

    userRef.on('child_added', function(data)
    {
        var studentx = data.val().TheStudents;
        var clazzID = data.val().ClassID;
        Object.keys(clazzID).forEach(function(key)
        {
            if(clazzID[key] == addclassID) {
                var myClass{};
                myClass.Studentname= user.displayName;
                checkclassID.child("MyStudents").set(myClass);
            }
            else
            {
                alert("Class doesnt exist!");
            }
        });
    }}


我认为以下代码不正确:

Object.keys(clazzID).forEach(function(key)
{
    if(clazzID[key] == addclassID) {
        var myClass{};
        myClass.Studentname= user.displayName;
        checkclassID.child("MyStudents").set(myClass);
    }


我错过了什么?学生加入特定班级的替代方式/代码应该是什么?

班级形象

javascript - 将学生添加到虚拟教室-LMLPHP

因此,在Classes下是user.uid,在user.uid下是push()发送的随机ID,它下面是教室的信息,例如ClassID,Teacher,TeacherID和TheClass(即类名)。如果用户学生成功加入教室,则MyStudents应该出现在相同级别的信息上:=> ClassID,Teacher,TeacherID,TheClass,MyStudent。在MyStudents下,在user / student加入教室之后,应该将其放置在该位置。

有关更多说明,请在下面将其注释掉。任何帮助将不胜感激。

JSON:

{
  "Accounts" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "displayName" : "Dodong Advices",
      "email" : "[email protected]",
      "status" : "Teacher"
    },
  }
  "Classes" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "-LMpvlBl3mEazhxaJwqb" : {
        "ClassID" : "6503-3503-6827",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "StackMates"
      },
      "-LMrfIBg8v-hj1k8X2Qf" : {
        "ClassID" : "7583-2402-2757",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "asdasd"
      },
      "-LMrfMV1aw3YNA0PfooR" : {
        "ClassID" : "8083-2712-3347",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "HAHAHAHA"
      }
    }
  }
}


我要完成的JSON:ps:我添加了一个新的学生用户

{
  "Accounts" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "displayName" : "Dodong Advices",
      "email" : "[email protected]",
      "status" : "Teacher"
    },
    "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
      "displayName" : "Sasuke Uchiha",
      "email" : "[email protected]",
      "status" : "Student"
    },
    "yJif4ReTxCcGmo682xWSG3L5MKE3" : {
      "displayName" : "Naruto Uzumaki",
      "email" : "[email protected]",
      "status" : "Student"
    },
    "yo9hQLNUtOhSVn9xEhBT7uDI72y1" : {
      "displayName" : "Itachi Uchiha",
      "email" : "[email protected]",
      "status" : "Teacher"
    }
  },
  "Classes" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "-LMpvlBl3mEazhxaJwqb" : {
        "ClassID" : "6503-3503-6827",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "StackMates"
      },
      "-LMrfIBg8v-hj1k8X2Qf" : {
        "ClassID" : "7583-2402-2757",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "OverFlowers"
      },
      "-LMrfMV1aw3YNA0PfooR" : {
        "ClassID" : "8083-2712-3347",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "CodeHard"
      }
    },
    "yo9hQLNUtOhSVn9xEhBT7uDI72y1" : {
      "-LMw39kVqsi5sYbudFSk" : {
        "ClassID" : "3514-2160-0317",
        "Teacher" : "Itachi Uchiha",
        "TeacherID" : "yo9hQLNUtOhSVn9xEhBT7uDI72y1",
        "TheClass" : "Akatsuki",
        "MyStudents" : {
           "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
              "StudentName" : "Sasuke Uchiha"
           }
        }
      },
      "-LMw3A_hP2vkKyaowt8J" : {
        "ClassID" : "7647-8387-3883",
        "Teacher" : "Itachi Uchiha",
        "TeacherID" : "yo9hQLNUtOhSVn9xEhBT7uDI72y1",
        "TheClass" : "Konoha",
        "MyStudents" : {
          "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
              "StudentName" : "Sasuke Uchiha",
          "yJif4ReTxCcGmo682xWSG3L5MKE3" : {
              "StudentName" : "Naruto Uzumaki"
           }
          }
        }
      }
    }
  }
}



  弗兰克先生守则的结果


{
  "Accounts" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "displayName" : "Dodong Advices",
      "email" : "[email protected]",
      "status" : "Teacher"
    },
    "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
      "displayName" : "Sweet Macaroni",
      "email" : "[email protected]",
      "status" : "Student"
    }
  },
  "Classes" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "-LMpvlBl3mEazhxaJwqb" : {
        "ClassID" : "6503-3503-6827",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "StackMates"
      },
      "-LMrfIBg8v-hj1k8X2Qf" : {
        "ClassID" : "7583-2402-2757",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "GoogleHub"
      },
      "7583-2402-2757" : {
        "MyStudents" : {
          "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
            "Studentname" : "Sweet Macaroni"
          }
        }
      }
    }
  }
}

最佳答案

为此,您必须知道两个UID:一个用于学生,一个用于老师。有了这些,代码可以非常简单:

function addclass() {
    var addclassID = document.getElementById("classroomID").value;
    var teacherUID = ...; // ?
    var studentUID = user.uid;
    var studentName = user.displayName;
    var classRef = firebase.database().ref().child('Classes').child(teacherUID).child(addclassID);
    var studentsRef = classRef.child("MyStudents");
    studentsRef.child(studentUID).set({ Studentname: studentName });
}


更新资料

正如评论:由于您的类已经具有自己的唯一ID,因此强烈建议您在存储它们时将其用作键。这就是上面的代码所做的。

在当前结构中,您首先必须查询以找到该类的键,然后将其写入其中。您的代码通过加载所有类然后循环来完成此操作,但是如果您查询,则更容易:

function addclass() {
    var addclassID = document.getElementById("classroomID").value;
    var teacherUID = ...; // ?
    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 });
    });
}

10-06 08:23