我编写了一个匹配系统,该系统的工作原理是这样的 --> 当创建操作进入 firestore 时,它与旧记录匹配,并且云功能会删除它们。但我想知道当创建 2 个新记录时,是否有机会将这 2 个与相同的旧记录匹配,因为删除过程需要一些时间?
另外,如果我用更新来做这个过程,它比删除快吗?
编辑:
这是我的 matchMaker Cloud 功能:
exports.matchMaker = functions.firestore
.document("WaitingRooms/{gameId}/Users/{matchId}")
.onCreate(async (snap, context) => {
const data = snap.data();
const gameId = context.params.gameId;
const data2 = await db.collection("Users").doc(data["userId"]).get();
const data3 = await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.where("userId", "!=", data["userId"])
.limit(1)
.get();
if(data3.docs[0])
{
let match = data3.docs[0].data()["userId"]
matchPN = await db.collection("Users").doc(match).get();
matchPushNotifications = matchPN.data()["pushNotificationsTokens"];
myPushNotifications = data2.data()["pushNotificationsTokens"];
await db
.collection("Users")
.doc(match)
.update({
waitingMatches: admin.firestore.FieldValue.arrayUnion({id:data["userId"]}),
});
await db
.collection("Users")
.doc(data["userId"])
.update({
waitingMatches: admin.firestore.FieldValue.arrayUnion({id:match}),
});
//SOME OTHER CODES
await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.doc(data3.docs[0].id)
.delete();
await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.doc(context.params.matchId)
.delete();
}
});
此代码工作正常,但问题是如果 Waiting Rooms/{gameId}/Users/
有 3 条记录,则代码匹配其中的 3 条,但我想匹配其中的 2 条,其中之一必须保留。
我知道云函数无法知道哪个匹配或不匹配,但我如何检查?