在Firebase云功能(Firestore)中获取文档ID

时间:2018-03-31 10:38:54

标签: firebase google-cloud-functions google-cloud-firestore

Firebase Cloud功能是否有办法查询包含字段和数据的文档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

            }
        });

enter image description here

提前致谢!

1 个答案:

答案 0 :(得分:1)

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

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

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

另见https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection