如何通过firebase DB运行一组数字并返回匹配?

时间:2017-06-22 20:31:12

标签: javascript csv firebase firebase-realtime-database

我想从CSV文件中检索唯一ID,并找到与我的firebase数据库中的ID匹配,然后将匹配项复制到另一个子项。

我创建了一个包含此数字格式的CSV文件 ID 75799757, 9744710, 79989647,

这是我的firebase数据库结构 Datab

到目前为止,我已经能够弄清楚如何使用此JS代码读取csv文件

      readFile = function () {
          var reader = new FileReader();
          reader.onload = function () {
            document.getElementById('out').innerHTML = reader.result;
          };
        // I assigned the IDs retrieved from the CSV file to an array named keys
          var keys = [reader.result];
          var promises = keys.map(function(key) {
              return firebase.database().ref("/Agents/").child(key).once("value");
            });
        // return the values for each ID

           Promise.all(promises).then(function(snapshots) {
              snapshots.forEach(function(snapshot) {
                console.log(snapshot.key+": "+snapshot.val());
              });
            });
        // start reading the file. When it is done, calls the onload event defined above.
          reader.readAsBinaryString(fileInput.files[0]);
      };

html文件

  <input type="file" class="upload">
   <section class="file">
  </section>

css文件

body {
padding: 3em;
}

.file {
border: solid 1px gray;
padding: 25px;
margin: 25px;
}

文件firebase调用不起作用,我能够从CSV文件中检索ID,但是我无法找到匹配的ID并且控制台记录它们的值。我究竟做错了什么?我该如何解决这个问题,以便它返回与数组中的ID匹配的firebase值?

1 个答案:

答案 0 :(得分:0)

有很多事情可能是错的。但跳出来的第一件事就是你拥有在onload()回调之外使用CSV的代码。这似乎是一个错误。

  readFile = function () {
      var reader = new FileReader();
      reader.onload = function () {
        document.getElementById('out').innerHTML = reader.result;

        // I assigned the IDs retrieved from the CSV file to an array named keys
        var keys = [reader.result];
        var promises = keys.map(function(key) {
            return firebase.database().ref("/Agents/").child(key).once("value");
        });
        // read the data from the database for each ID
        Promise.all(promises).then(function(snapshots) {
          snapshots.forEach(function(snapshot) {
            console.log(snapshot.key+": "+snapshot.val());
          });
        });
      };

      // start reading the file. When it is done, calls the onload event defined above.
      reader.readAsBinaryString(fileInput.files[0]);
  };