YYY
YYY

Reputation: 1779

Getting document ID in Firebase Cloud Functions (Firestore)

Is there a way in Firebase Cloud Function to query for a document ID with Field and data?

In Android I would do something like this:

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

Thanks in advance!

Upvotes: 3

Views: 6643

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83191

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

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

Basically in Cloud Functions you write code for node.js: have a look at the documentation https://firebase.google.com/docs/firestore/query-data/queries and choose node.js in the code snippets.

See also https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection

Upvotes: 5

Related Questions