I have the following Firestore set up:
- Users / uid / following / followingPersonUid /
- Users / uid / followers / followerPersonUid /
So if User A would follow User B then User A would get added to the followers sub collection of User B also User B will get added to the Following Subcollection of User A
But let's say User A updates his profile info( name, photo, userName, etc. ). Then his user doc will change in his document but this needs to change everywhere wherever he is a follower in the Followers sub-collection of other users like User B or E & F.
Can this be done through cloud functions? I have created an onCreate() trigger for cloud functions but the function doesn't know the list of other users(uids) where he is a follower so I can't apply this change wherever it's needed.
This is my function in Firebase CLI, This is a firestore .onUpdate() trigger. I ve commented the place at which I am stuck
export const onUserDocUpdate = functions.region('asia-
east2').firestore.document
('Users/{userId}').onUpdate((change, context) => {
const upDatedUserData = change.after.data()
const newName = upDatedUserData?.name
const profilePhotoChosen = upDatedUserData?.profilePhotoChosen
const updatersUserId = upDatedUserData?.uid
const newUserName = upDatedUserData?.userName
//This is where I am stuck, I have the updated document info but how do
//I find the other documents at firestore that needs updation with this
//updated information of the user
return admin.firestore()
.collection('Users').doc('{followeeUserId}')
.collection('Followers').doc(updatersUserId)
.set({
name: newName,
userName: newUserName,
profilePhotoChosen: profilePhotoChosen,
uid: updatersUserId
})
})
Should I use a callable function instead wherein the client can send a list of following userIds that need to be updated.
following
subcollection? Either way: it's probably easier to help if we see the minimal code that reproduces where are stuck. – Frank van Puffelen