Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在CloudFireStore中检查文档是否包含属性?_Node.js_Firebase_Google Cloud Firestore - Fatal编程技术网

Node.js 如何在CloudFireStore中检查文档是否包含属性?

Node.js 如何在CloudFireStore中检查文档是否包含属性?,node.js,firebase,google-cloud-firestore,Node.js,Firebase,Google Cloud Firestore,我想知道是否有办法检查Cloud Firestore文档中是否存在属性。类似于document.contains(“property\u name”)或如果存在文档属性。要解决此问题,只需检查DocumentSnapshot对象的空值,如下所示: var yourRef = db.collection('yourCollection').doc('yourDocument'); var getDoc = yourRef.get() .then(doc => { if (

我想知道是否有办法检查Cloud Firestore文档中是否存在属性。类似于
document.contains(“property\u name”)
或如果存在文档属性。

要解决此问题,只需检查
DocumentSnapshot
对象的空值,如下所示:

var yourRef = db.collection('yourCollection').doc('yourDocument');
var getDoc = yourRef.get()
    .then(doc => {
      if (!doc.exists) {
        console.log('No such document!');
      } else {
        if(doc.get('yourPropertyName') != null) {
          console.log('Document data:', doc.data());
        } else {
          console.log('yourPropertyName does not exist!');
        }
      }
    })
    .catch(err => {
      console.log('Error getting document', err);
    });
我想你指的是提问。 仍然无法检查Firestore中是否存在某些字段。但您可以添加另一个值为true/false的字段

查看此链接了解更多信息


您可以使用
中的
操作符,就像下面的代码片段一样

  const ref = admin.firestore().collection('yourCollectionName').doc('yourDocName')
  try {
    const res = await ref.get()
    const data = res.data()    
    if (!res.exists) {
      if (!("yourPropertyName" in data)) {
         // Do your thing
      }
    } else {
         // Do your other thing
    }
  } catch (err) {
    res.send({err: 'Something went terribly wrong'})
  }

嗨,戴安!你试过我上面的解决方案了吗?Alex,对不起。。。我做了,但我忘了将其标记为解决方案,但为我工作。谢谢
  const ref = admin.firestore().collection('yourCollectionName').doc('yourDocName')
  try {
    const res = await ref.get()
    const data = res.data()    
    if (!res.exists) {
      if (!("yourPropertyName" in data)) {
         // Do your thing
      }
    } else {
         // Do your other thing
    }
  } catch (err) {
    res.send({err: 'Something went terribly wrong'})
  }