如何检查变量是否是 firstore firebase 函数中的文档实例或集合引用?

人气:765 发布:2022-10-16 标签: firebase google-cloud-firestore google-cloud-functions

问题描述

在将文档引用作为输入传递的 Firebase 函数中,如何检查该引用是针对文档还是集合?

In a Firebase function that is passed a document reference as input, how can I check if that reference is for a document or a collection?

例如:

if(data.ref instanceof FIRESTORE_COLLECTION_REFERENCE) {
  //do something...
} else if (data.ref instanceof FIRESTORE_DOCUMENT_REFERENCE) {
  //do something else...
}

如果这是一种允许的检查方式,那么对该数据类型的正确调用是什么?如果不允许,我该如何检查?

What is the correct call to that data type if this is an allowable means to check? If not allowed, how can I check this?

推荐答案

首先,确保您已正确安装 Firebase.现在,为了使其工作,您应该使用以下两个导入:

First of all, make sure you have Firebase correctly installed. Now, in order to make it work, you should use the following two imports:

import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.CollectionReference;

之后,您可以使用以下代码行:

Right after that, you can use the following lines of code:

if(data.ref instanceof CollectionReference) {
    //do something...
} else if (data.ref instanceof DocumentReference) {
    //do something else...
}

148