0
votes

More explanation:

  1. A document gets created on iOS. It contains a city and a type.
  2. In CloudFunctions, I detect the document creation/update via the onWrite trigger.
  3. I want to query other FireStore documents for Users meeting the City + type criteria. Then I want to send them notifications.

Here is my code:

exports.NewRequestCreated = functions.firestore.document('/Requests/{documentId}').onWrite(event => {
// Get an object with the current document value

var myRequest = event.data.data();

// Get an object with the previous document value (for update or delete)
// Note that previous will be null if no old value exists.
if (event.data.previous) {
  var oldDocument = event.data.previous.data();
}

//Get list of donors that can match this request
var compatibleTypesArray = getCompatibeUsers(myRequest.Type);
var matchingUsersArray = [];


var usersRef = functions.firestore.document('/Users');//functions.firestore.document('/Users');
var matchingUsersQuery = usersRef.where('city', '==', myRequest.city).where('type', '==', myRequest.type)
var user = matchingUsersQuery.get()
.then(snapshot => {
    snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
    });
})
.catch(err => {
    console.log('Error getting documents', err);
});

});

But this is failing with the following error in Functions Console:

TypeError: usersRef.where is not a function at exports. exports.NewRequestCreated Created.functions.firestore.document.onWrite.event (/user_code/index.js:45:40) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:695:26 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1
In addition to Doug's answer, note that Cloud Functions that perform asynchronous processing (such as get()) must return a Promise. This is explained in the documentation. Because you will be adding code to send the FCM messages, which is also an asynchronous operation, you will need to also become familiar with chaining Promises. The video in the linked docs is helpful.Bob Snyder

1 Answers

1
votes

usersRef is a DocumentReference. As you'll see in the API docs, there's no where() method on that class.

Perhaps you meant to instead get a CollectionReference object using collection(), which you can then query for documents using where() method.

functions.firestore.collection('/Users');