我想从我的 Firestore 数组中删除多个元素:
var eliminatedThisRound = []
for (const player in players){
if (players[player].eliminated === false && players[player].answer !== answer) {
eliminatedThisRound.push(players[player].uid);
}
}
var update = {
roundFinished: true,
nextRound: date.valueOf() + 12000,seconds
players: updatedPlayers,
remainingPlayers: admin.firestore.FieldValue.arrayRemove(eliminatedThisRound)
}
await t.update(gameRef, update);
上面返回这个错误:
transaction failure: Error: Element at index 0 is not a valid array element. Nested arrays are not supported.
所以如果我知道这些值就好了,因为我可以这样做:
remainingPlayers: admin.firestore.FieldValue.arrayRemove("player1", "player2")
但是我还没有找到制作arrayRemove()
参数的方法动态的。
有什么想法吗?
最佳答案
您需要使用 Spread operator ,如下所示,以便将 eliminatedThisRound
的所有元素作为参数传递给 arrayRemove()
方法。
var eliminatedThisRound = []
for (const player in players){
if (players[player].eliminated === false && players[player].answer !== answer) {
eliminatedThisRound.push(players[player].uid);
}
}
var update = {
// ...
admin.firestore.FieldValue.arrayRemove(...eliminatedThisRound)
}
await t.update(gameRef, update);
请注意,您应该在数组中至少有一个元素,否则您将使用 0 个参数调用 arrayRemove()
,而它至少需要 1 个参数。因此,您可以在将 remainingPlayers
属性分配给 update
对象之前检查数组长度。
https://stackoverflow.com/questions/65547447/