如何检查文档是否在Cloud Firestore中包含属性? [英] How to check if a document contains a property in Cloud Firestore?

查看:48
本文介绍了如何检查文档是否在Cloud Firestore中包含属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以检查Cloud Firestore文档中是否存在属性. document.contains("property_name")之类的东西或是否存在文档属性.

I want to know if there is a way to check if a property is present in a Cloud Firestore document. Something like document.contains("property_name") or if there is a document property exist.

推荐答案

要解决此问题,您可以像下面这样简单地检查DocumentSnapshot对象的无效性:

To solve this, you can simply check the DocumentSnapshot object for nullity like this:

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);
    });

这篇关于如何检查文档是否在Cloud Firestore中包含属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆