【问题标题】:How to iterate through nested subcollections in flutter web?如何遍历flutter web中的嵌套子集合?
【发布时间】:2021-11-08 19:07:07
【问题描述】:

我有一组商家,每个商家都有一个文档和一个集合,每个集合都有嵌套文档和子集合。

我创建了一个循环,这样每次用户点击 div 时,都会调用该函数来获取数据库中下一个集合的快照。

但是该函数没有调用,因为我试图将每个文档和集合的值存储在一个数组中,然后在函数中调用该数组以将数据库链接到 Web 应用程序(如果这有意义的话) .

但它抛出了 Uncaught TypeError: Cannot read properties of undefined (reading 'onSnapshot') 的错误。关于如何调用数据库中的下一个文档和子集合的任何想法?

console.log 的连接输出为: 'doc(建筑材料).collection(建筑材料)'

但是来自 ref 的 console.log 的输出是: '未定义'

代码如下:


 $(".outer-body").click(function(){

  var src = $(this).text().trim();
      sourceArray.push(src);

      var doc;
      var collection;

      var dataArray = [];

      for (var x = 0; x < sourceArray.length; x++){
          var sourceX = sourceArray[x];
          if(sourceArray[x] == sourceArray[0]){
            doc = "doc(" + sourceX + ")";
          } else {
            doc = ".doc(" + sourceX + ")";
          }
          collection = ".collection(" + sourceX + ")";
          dataArray.push(doc + collection);
      }

      var join = dataArray.join('');
      console.log(join);

      const ref = db.collection("all_merchants").doc(customClaim).collection(customClaim).join

      console.log(ref);


      ref.onSnapshot((querySnapshot) => {
        console.log(doc.id, '=>', doc.data());
    });

 }

【问题讨论】:

    标签: javascript jquery firebase google-cloud-firestore


    【解决方案1】:

    要使文档引用动态化,您必须更新函数的参数。

    'function(argument)' 可能无法按预期工作。另一方面,这将是:function('argument')。请务必记住,这些是函数,而不是字符串。

    QueryDocumentSnapshot 的 API 表面与 DocumentSnapshot 的相同。 exists() 方法将始终返回 true,而 getData() 永远不会返回 null,因为查询结果只包含现有文档。

    我建议使用querySnapshot.exists() 查看您要查找的文档是否确实存在。这样,您将能够更有效地处理任何错误。

    请记住验证当前版本是 V9,因为这样可以更轻松地为您提供支持。

    在 JavaScript 中包含How to Avoid Getting "Cannot read property of undefined" 的链接也会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2017-08-19
      • 2014-11-02
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2018-07-12
      相关资源
      最近更新 更多