在 Firebase Cloud Functions (Firestore) 中获取文档 ID

YYY*_*YYY 3 firebase google-cloud-functions google-cloud-firestore

Firebase Cloud Function 中是否可以通过字段和数据查询文档 ID?

在 Android 中,我会做这样的事情:

FirebaseFirestore fs = FirebaseFirestore.getInstances();

static final String mInterfaceId = "57ZNmAIldUPAc6ZWggvF";

fs.collection("users").whereEqualTo("interface_id", mInterfaceId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {

                for(DocumentSnapshot doc : task.getResult())
                    userId[0] = doc.getId();
                
                if(userId.length==0)
                    Log.e("Error: ID Not found");
                else
                    if(userId.length>1)
                        Log.e("Error: More than one ID found");
                    else
                        // Continue Process with Found ID
                
            }
        });
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

提前致谢!

Ren*_*nec 5

var usersRef = db.collection('users');

var queryRef = usersRef.where('interface_id', '==', mInterfaceId).get()
.then(snapshot => {
    snapshot.forEach(doc => {
        ...
    });
})
.catch(err => {
    ...
});
Run Code Online (Sandbox Code Playgroud)

基本上在 Cloud Functions 中,您为 node.js 编写代码:查看文档https://firebase.google.com/docs/firestore/query-data/queries并在代码片段中选择 node.js。

另请参阅https://firebase.google.com/docs/firestore/query-data/get-data#g​​et_multiple_documents_from_a_collection