如何在评估中检索Evaluator的数据?

时间:2017-05-21 08:15:42

标签: javascript firebase web firebase-realtime-database

我目前遇到了问题。我是一个firebase新手。我只想检索组(来自评估),评论和总计(来自评估者)。但问题是它不起作用。这是我的json文件

  {
 "Evaluation" : {
"-Kk8jZeZHW1s_zoTftcE" : {
  "Evaluator" : {
    "Y4WQjEH8c2SXYgphZTbOkSAg6272" : {
      "comment" : "good",
      "total" : 100,
      "username" : "dr z"
    }
  },
  "group" : "Survivor",
  "leader" : "chloe",
  "lect1" : "v@gmail.com",
  "lect2" : "s@yahoo.com",
  "posterID" : "58",
  "school" : "sds",
  "title" : "Atomic2",

},

这是我的try.js

var ref = firebase.database().ref("Evaluation");
ref.once('value', function(Evaluationsnapshot) {
var Evaluationchild = Evaluationsnapshot.child("Evaluation").key;   
var Evaluatorchild = firebase.database().ref('Evaluation/' + Evaluationchild 
+ '/Evaluator');

Evaluationsnapshot.child(Evaluationchild).child('Evaluator').child(Evaluator
child).forEach(function(Evaluatorchildsnapshot) {
    console.log(Evaluatorchildsnapshot.key);
    var commentdata = Evaluatorchildsnapshot.val().comment;
    var totaldata = Evaluatorchildsnapshot.val().total;
    var userdata = Evaluatorchildsnapshot.val().username;

    console.log(" comment:" + commentdata + " total:" + totaldata + " 
    username:" + userdata);

    });
});

迫切需要帮助。请帮助我。任何反馈,非常感谢。

1 个答案:

答案 0 :(得分:0)

稍微重构了一下代码,这可能对你有帮助。

var ref = firebase.database().ref('Evaluation');
ref.on('value', function(snapshot) {
  var records = snapshot.val();
  var recordKeys = Object.keys(records);
  recordsKeys.forEach(function(recordKey) {
    var groupName = records[recordKey].group;
    var groupData = records[recordKey].total;
    console.log(groupName + " was " + groupData);
  });
});