【问题标题】:Data from firebase is coming in a shuffled manner?来自 firebase 的数据正在以洗牌的方式出现?
【发布时间】:2020-08-05 06:59:48
【问题描述】:

我正在尝试使用来自 firestore 集合的数据填充表。数据来得很完美,但是当我在控制台记录数据时,每一行的数据都以打乱的方式出现。让我告诉你我想要什么以及它是如何实现的。

假设我在集合 ABC 中有 4 个文档,键为 _name, _contact, _comments

预测输出:

{
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' },
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' },
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' },
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' }
}

实际输出:

{
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' },
  { _contact: 'xyz', _name: 'ABC', _comment: 'def' },
  { _name: 'ABC', _comment: 'def', _contact: 'xyz' },
  { _name: 'ABC', _contact: 'xyz', _comment: 'def' }
}

JS 代码

const tableObj = document.getElementById('quoteVal');
dbref.get().then(snap => {
    snap.forEach(item => {
        data = item.data();
        let ele = document.createElement('tr');
        for(let key in data) {
            if(data.hasOwnProperty(key)) {
                ele.innerHTML += '<td>' + data[key] + '</td>';
            }
        }
        tableObj.append(ele);
    });
});

请帮帮我。我不明白为什么会发生这种情况,因为我是 firebase 新手。

【问题讨论】:

    标签: javascript jquery firebase google-cloud-firestore nosql


    【解决方案1】:

    您的代码不应依赖于文档中字段的迭代顺序。 没有保证订购

    如果您需要订单,则应在生成输出之前自行对字段进行排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2020-11-27
      • 2019-03-21
      相关资源
      最近更新 更多