【问题标题】:How can you check if a field exists in a document/documentSnapshot or not?如何检查文档/documentSnapshot 中是否存在字段?
【发布时间】:2020-02-11 19:52:57
【问题描述】:

documentSnapshot 文档中有一个名为 contains(string fieldName) 的公共方法。 https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot#public-method-summary

但是当我在我的代码中使用这个方法时,我总是得到一个错误代码,比如 TypeError: documentSnapshot.contains is not a function

我弄错了吗?有解决办法吗?

这是我正在尝试做的一个示例。

let citiesRef = db.collection('cities');
let allCities = citiesRef.get()
  .then(snapshot => {
    snapshot.forEach(doc => {
      if(doc.contains('states'){
      console.log(doc.id, '=>', doc.data());
     };
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    您使用的是 Javascript,并且您链接的参考是针对 Android 的。在Javascript中,可以使用get()的方法:

       snapshot.forEach(doc => {
          if(doc.get('states') != null){
          console.log(doc.id, '=>', doc.data());
         };
        });
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 2023-03-07
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 2019-02-19
      • 1970-01-01
      • 2015-11-27
      相关资源
      最近更新 更多