首页 文章

为什么非自动生成的文档ID在Firestore控制台中以斜体显示?

提问于
浏览
6

当我添加一个包含我自己的文档ID(不是自动生成)的文档时,文档Id节点以斜体显示,如Firestore控制台的屏幕截图所示 . 这背后的原因是什么?

我添加数据的代码是

const billingRef = db
      .collection('billing/test/2017/months/11')
      .doc();

  billingRef
      .set({ name: 'ABC' })
      .then(_ => {
        console.log('saved');
      })
      .catch(err => {
        console.log(err);
      });

上面的代码成功添加了一个节点,但是以斜体添加节点“test”和“months” .

截屏1
enter image description here

截屏2
enter image description here
截屏3
enter image description here

我的查询在firestore中为这些记录产生零结果,代码如下 . 如何查询计费下的所有节点?

db.collection("billing").get().then(function(querySnapshot) {
    console.log(querySnapshot.size) // this is always 0
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});

1 回答

  • 4

    按照上面的评论,您将在Firestore控制台中看到,对于 italic 中的文档,有一个小文本说 "This document does not exist, it will not appear in queries or snapshots" ,对于 non-italic 它说 "This document has no data" ,所以直觉是当文档是在没有任何字段的代码中创建的那么它是"null"(子集合不计算在内) . 如果添加并删除了一个Field,那么Document只是空的而不是null .

    由于您对结算下的凭证的查询是斜体(“空”或不存在),如上文所述,它们不会出现在查询中 .

    解决方案是通过Firestore控制台添加文档,因为这里将Documents创建为空,或者如果在代码中添加一个Field,如果不需要则可以再次删除它,那么Documents将出现在查询中 .

相关问题