Firestore中的重复文档是否可以通过文档编辑的云功能进行更新



我设置了以下Firestore:

  • 用户/uid/flowing/flowingPersonUid/
  • 用户/uid/flowers/flowersPersonUid/

因此,如果用户A将跟随用户B,则用户A将被添加到用户B的跟随者子集合中,用户B也将添加到用户A的跟随者子集合中

但假设用户A更新了他的个人资料信息(姓名、照片、用户名等(。然后他的用户文档将在他的文档中更改,但无论他是其他用户(如用户B或E&F.

这可以通过云功能实现吗?我已经为云函数创建了一个onCreate((触发器,但该函数不知道他是追随者的其他用户(uid(的列表,所以我不能在需要的地方应用此更改。

这是我在Firebase CLI中的函数,这是一个firestore.onUpdate((触发器。我已经评论了我被卡住的地方

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
})
})

我应该使用一个可调用的函数吗?在该函数中,客户端可以发送以下需要更新的用户ID的列表。

据我所知,用户会更新他们的配置文件,然后你还想更新他们所有追随者数据中的配置文件。由于你同时拥有追随者和追随者,你应该能够阅读触发云功能的用户的子集合:

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
const userDoc = change.after.ref.parent; // the user doc that triggered the function
const followerColl = userDoc.collection("Followers");
return followerColl.get().then((querySnapshot) => {
const promises = querySnapshot.documents.map((doc) => {
const followerUID = doc.id;
return admin.firestore()
.collection('Users').doc(followerUID)
.collection('Followees').doc(updatersUserId)
.set({
name: newName, 
userName: newUserName,
profilePhotoChosen: profilePhotoChosen,
uid: updatersUserId
})
});
return Promise.all(promises);
});
})

可能是我有一些打字错误/语法错误,但语义应该相当扎实。我最不确定的是你维护的follower/followee逻辑,所以我使用了对我来说最有意义的集合名称,这可能与你的相反。

最新更新