Get Document in Firestore Cloud Function

get all documents in collection firestore
firestore get document field
firestore get document by id
firestore cloud functions example
firebase cloud functions firestore query
firestore document
write to firestore from cloud function
firestore get all collections

I am creating a Firebase cloud function that messages a particular user when their rating has been updated (a trigger from Firestore).

So far I have;

// Send New Rating Notifications
exports.sendNewRatingNotification = functions.firestore.document('users/{userID}/ratings/{ratingID}').onWrite((context) => {

    // Get {userID} and field of fcmToken and set as below

    var fcmToken = fcmToken;
    var payload = {
        notification: {
            title: "You have recieved a new rating",
            body: "Your rating is now..."
        }
    }

    return admin.messaging().sendToDevice(fcmToken, payload).then(function(response) {
        console.log('Sent Message', response);
    })
    .catch(function(error) {
        console.log("Error Message", error);
    })
})

I need to access the {userID} documents' fcmToken field to use below how to I approach this from using the wildcard {userID}

This is spelled out in the documentation for wildcards:

exports.sendNewRatingNotification =
functions.firestore.document('users/{userID}/ratings/{ratingID}').onWrite((change, context) => {
    const userID = context.params.userID
})

Note that the first argument to the callback is not context, it's Change object that describes the before and after state of the document. The second argument is a context that contains the parameters of the change.

Extend Cloud Firestore with Cloud Functions, “Get Firestore data from a Cloud Function” is published by Akash Joshi const docRef = db.collection('collection name').doc('document name')� With Cloud Functions, you can handle events in Cloud Firestore with no need to update client code. You can make Cloud Firestore changes via the DocumentSnapshot interface or via the Admin SDK. In a typical lifecycle, a Cloud Firestore function does the following: Waits for changes to a particular document.

If you check line number 30-31 of functions-samples/fcm-notifications repository.

You will get using:

  const userID= context.params.userID;

Hope it will helps you.

Thank you.

Get Firestore data from a Cloud Function | by Akash Joshi, This is spelled out in the documentation for wildcards: exports. sendNewRatingNotification� It will store the values into a new document in firestore using the add() function (which auto-assigns an ID). And the function is also listening for a GET request with an id in the querystring. It will lookup that document in firestore and, if found, return the document. NOTE: you probably want more input sanitation for a production application.

I've implemented the process from the accepted answer above.

My Firebase Function is as follows;

exports.sendNewRatingNotification = functions.firestore.document('users/{userID}/ratings/{ratingID}').onWrite((change, context) => {
    var userRef = admin.firestore().collection('users').doc(context.params.userID);
    return userRef.get().then(doc => {
        if (!doc.exists) {
            console.log('No such document!');
        } else {
            const data = change.after.data();
            const fcmToken = doc.data().fcmToken;
            var payload = {
                notification: {
                    title: "New Rating",
                    body: "You have recieved a " + data["rating"] + "* rating"
                }
            }
            return admin.messaging().sendToDevice(fcmToken, payload).then(function(response) {
                console.log('Sent Message:', response);
            })
            .catch(function (error) {
                console.log('Error Message:', error);
            });
        };
    });
});

Get Document in Firestore Cloud Function, And the function is also listening for a GET request with an id in the querystring. It will lookup that document in firestore and, if found, return the� Get Document in Firestore Cloud Function. Ask Question Asked 5 months ago. Active 28 days ago. Viewed 198 times 2. I am creating a Firebase cloud function that

Use Cloud Firestore with Cloud Functions, Reading Data. When a function is triggered, you might want to get data from a document that was updated, or get the data prior to update. You� When a function is triggered, you might want to get data from a document that was updated, or get the data prior to update. You can get the prior data by using change.before.data() , which contains the document snapshot before the update.

Extending Firestore with Cloud Functions, In this tutorial, we're going to be exploring Firebase Cloud Functions to run To get started, the first thing you need to do is to install the Firebase CLI, to do that, We're using the functions.firestore package to listen to the document located at: Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Learn How To Use Cloud Functions Triggers for Firestore �, Firestore cloud function that maintains a document field that contains number return userDoc.ref.collection('my-collection').get().then(collectionSnapshot => {. A Firebase Cloud Function configured with a Firestore database trigger makes it possible to perform this task automatically whenever a document changes. This demo simulates the relationship between blog posts and a subcollection of comments.

Comments
  • thanks, @Doug can I access the fcmToken field directly (const fcmToken = context.param.fcmToken) or do I need to get the document data of the user then access the fcmToken field.
  • There is no token field in the context. You'll have to store and retrieve that per user.