0
votes

I am trying write an aggragation function to update a few details on a value change of a nested object of a doc in wildcard.

exports.createAccessDetails = functions.firestore.document('classrooms/{classId}/access/{accessType}').onWrite((snap, context) => {
  // const data = snap.data();
  const { classId, accessType } = context.params;
  console.log(snap);
  console.log(classId, accessType);
  return null;
});

Then updating the object like on a button click

function setAccess() {
  const date = Date.now();
  Firebase.firestore.collection('classrooms')
    .doc('1570776211111').update({
      [`access.writeAccess.${date}`]: true
    });
}

What am I doing wrong? Why the function didnt trigger?

2
Can you give more details on what you are exactly trying to achieve. As pepe explained in his answer, you update to collection('classrooms').doc('1570776211111') but your Cloud Function is defined to be triggered when you write to collection('classrooms').doc('1570776211111').collection('access').doc('xyz'). So were do you exactly want to update? in collection classrooms or in the access sub-collection of a classroom?Renaud Tarnec
So, classrooms has a huge object where I'm updating a key access which has another key called writeAccess or another type, with different data. Currently, I didn't create access as a different collection, I'm pushing it as an object of the collection classrooms. Now from Pepe's comment I understand if I have to listen for a change in the object of access/{accessType} I have to makeaccess a subclass of classrooms, is that correct? If so, I have to rewrite my query too? Currently querying based on firestore.collection('classrooms').where(access.readAccess.${id}, '==', true)?Subhendu Kundu

2 Answers

0
votes

From your comment above, it appears that you want to write (update) a document of the classrooms collection. More precisely, update the access field which is of type Map (see doc for Firestore types).

So your Cloud Function shall simply be triggered when you update this document and to get the value of the access map you will call snap.data().access.

exports.createAccessDetails = functions.firestore.document('classrooms/{classId}').onWrite((snap, context) => {
  const data = snap.data();

  const accessMap = snap.data().access;
  const writeAccessMap = accessMap.writeAccess

  //........

  return null;
});

Update following your comment below:

I don't have a full view of your exact business requirements (in particular I see that you mention writeAccess and readAccess) but you could maybe have the following approach:

For each classroom doc you create two sub-collections: writeAccess and readAccess. Each time you want to save an access you write a new document in one of these collections.

You can then query the sub-collections to find the docs where a given id has read or write access. Then from the QueryDocumentSnapshot returned by these queries you can find the parent document (through the ref property).

However, this means that you will have an extra read to get the classroom details from the results of the queries to the writeAccess and readAccess collections.

0
votes

The update operation will not trigger the cloud function.Because you wrote the trigger function for a sub collection named access and you are making an upadation on a root collection called classroom.So the above written trigger function will trigger only for sub collection called access not for the root collection called classroom.Hope this will help you