An Nguyen
An Nguyen

Reputation: 117

Cloud function: No document found

I'm using cloud function to check if specific document exists but it did not work. No document found even there was. The code is below:

exports.onUserAppCreated = functions.firestore.document('users/{userId}/first_col/{colId}')
  .onCreate((snap, context) => {
    const data = snap.data();

    const colId = data.colId;
    console.log(colId);
    var docRef = db.collection('users/{userId}/somecollection');

    let query = docRef.where('colId', '==', colId).get().then(doc => {
        if (doc.exists) {

            console.log("Document data:", doc.data());
            let tracksRef = db.collection('users/{userId}/othercolllection');
            tracksRef.where('otherId', '==', colId).get()
                                          .then(transSnapshot => {
                                            if (!transSnapshot.exists) {


                                                transSnapshot.ref.set({
                                                otherId: colId,
                                                time:admin.firestore.FieldValue.serverTimestamp()
                                                });
                                            }
                                            return transSnapshot;
                                          }).catch(error => {
                                                   console.log(error);
                                                   //response.status(500).send(error);
                                                })
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
            return;
        }
        return doc;
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });

Has I done something wrong here?

Upvotes: 2

Views: 300

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

I understand that you want to get the value of colId from the {colId} wildcard which is in the path 'users/{userId}/first_col/{colId}'. You should use the context object as follows:

exports.onUserAppCreated = functions.firestore.document('users/{userId}/first_col/{colId}')
  .onCreate((snap, context) => {
    const data = snap.data();

    const colId = context.params.colId;

    //....

});

Note that snap is the DocumentSnapshot corresponding to the document that triggered the Cloud Function. So snap.data() gives you an object containing the fields of this document, and therefore data.colId is undefined (unless you have saved the document id in a colId field in your document).

Note also that you could get the value of colId through the snap object by doing snap.id, but for the other wildcard, i.e. userId, you will need to use context.params.


In addition, note that you don't take into account the promises returned by the asynchronous methods of the Admin SDK (get(), set()). It is very important that you correctly return those promises, see the corresponding doc.

Upvotes: 2

Related Questions