0
votes

Im currently working on a firebase cloud function which needs to work with my firestore. The structure of my firebase is the following:

chatrooms/{chatID}/{chatID}/anymessage

Both chatIDs are same, they resemble an individual chatroom and each chatroom has anymessages inside. Also, each chatroom has a "hello" document in which I have crucial data I need to use inside my cloud function because I need to extract a key from there so I can send a push notification.

This is my cloud function Im trying to deploy currently:

exports.createpushnotification = functions.firestore.document('chatrooms/{chatID}/{chatID}/hello').onUpdate((change, context) => {
     const keyofuser1 = change.after.data().partner1pushkey; //key to use for push notification

     fetch("https://exp.host/--/api/v2/push/send", {
         method:"POST",
         headers:{
             "Accept": "application/json",
             "Content-Type": "application/json"
         },
         body:JSON.stringify({"to":keyofuser1, "body":"New Message!"})
     })
     

 })

Basically I want a cloud function which goes into each chatroom directory, fetches the "hello" file from there and waits for it to update and when it does, execute the fetch operation. Unluckily I get an error and even with --debug I can not see any error log that could help me:

Functions deploy had errors with the following functions: createpushnotification(us-central1)

To try redeploying those functions, run: firebase deploy --only "functions:createpushnotification"

To continue deploying other features (such as database), run: firebase deploy --except functions [2021-08-23T07:33:26.669Z] Error during update for projects/*********/locations/us-central1/functions/createpushnotification:

Error: Functions did not deploy properly.

What am I doing wrong here?

1
You are not returning the promise. Try adding return before fetch(...). Also can you share a screenshot of your Firestore doc? chatrooms/{chatID}/{chatID}/anymessage why do you have chatID twice? - Dharmaraj
"I want a cloud function which goes into each chatroom directory, fetches the "hello" file from there" => Your current Cloud Function will not do that. It will be triggered only when one hello document will be modified. Can you confirm you want your Cloud Function to read each chatroom collection when it executes? - Renaud Tarnec
@Renaud Tarnec Yes. I want it to go into multiple locations and listen to the "hello" files, as you say. - Kubaghetto the fresh Testobun
@Dharmaraj I have ChatID twice because I needed to structure the Firestore in a way that at the end I have a document. Would I only use "one" ChatID I would have a collection at the endpoint and not documents. - Kubaghetto the fresh Testobun
@KubaghettothefreshTestobun I'm still not sure to understand your exact goal. Do you want to listen to any update that occurs to any hello document OR you want to loop over ALL the hello documents in one operation? - Renaud Tarnec

1 Answers

1
votes

Your problem comes from the fact that you use twice the same wildcard identifier, i.e. chatID, when defining your Cloud Function.

Changing the code to use two different identifiers, as follows

exports.createpushnotification = functions.firestore.document('chatrooms/{chatDocID}/{chatSubcollectionID}/hello').onUpdate(..);

should do the trick.


In addition, since the fetch operation is asynchronous, and since your Cloud function is a background Cloud Function, you need to return the promise returned by fetch() in order to indicate to the Cloud Function platform that it can terminate the function. See the doc for more details.

Do as follows:

return fetch("https://exp.host/--/api/v2/push/send", {..})