In my Firestore setup, I have the following structure:
- Users / uid / following / followingPersonUid /
- Users / uid / followers / followerPersonUid /
When User A follows User B, User A is added to the followers sub-collection of User B and User B is added to the following sub-collection of User A.
However, if User A updates his profile information (name, photo, username, etc.), his user document changes. This change needs to be reflected in all the places where he is a follower in other users' Followers sub-collections, such as User B or E & F.
Is it possible to achieve this using cloud functions? I have created an onCreate() trigger for cloud functions, but the function does not know the list of other users' uids where User A is a follower, making it difficult to apply the necessary update.
Shown below is the code snippet from Firebase CLI, which includes the .onUpdate() trigger for Firestore. The issue arises when trying to identify other documents in Firestore that require updating with the updated user information.
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
//Struggling here to find other documents in firestore that need
//updating with the latest user information
return admin.firestore()
.collection('Users').doc('{followeeUserId}')
.collection('Followers').doc(updatersUserId)
.set({
name: newName,
userName: newUserName,
profilePhotoChosen: profilePhotoChosen,
uid: updatersUserId
})
})
Would it be better to use a callable function instead where the client can send a list of following userIds that need to be updated?