问题描述
以下场景:
创建测验应用程序。 每当用户结束测验时,他的分数就会更新。 在本次更新中,firebase 函数会更新所有用户的排名:
exports.onMessageCreate = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
var test= admin.firestore().collection('users');
test.orderBy('totalScore', 'desc').get().then(querySnapshot => {
var rank = 1;
querySnapshot.forEach(user => {
user.ref.set({globalRanking: rank}, {merge: true}).then(res => { });;
rank++;
});
});
return null;
});
问题是,使用documentSnapshot.ref.set用户将再次更新,因此每次完整的 firebase 功能都会再次触发。
我怎样才能避免这种情况?
1楼
Mocas
3
2019-02-21 13:25:24
做到这一点,当用户结束测验时,他会更新或推送到不同的节点。 该功能监控该节点并更新所有用户排名