如何将数组传递到 firebase firestore arrayUnion()
function ?
当我尝试传递数组时出现此错误。
错误 Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
示例
let myArray = ["1", "2", "3"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion(myArray)
});
最佳答案
我最终找到了使用 Function.prototype.apply() 的答案在另一个 stack overflow answer .
Function.prototype.apply() 示例
let myArray = ["1", "2", "3"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion.apply(this, myArray)
});
ECMAScript 6 示例
使用 spread argument
let myArray = ["1", "2", "3"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});
使用上述任一方法传递数组时,Firestore 只会将新的数组元素添加到 Firestore 数组中尚不存在的元素中。
例如,运行上面,然后运行以下
let myArray = ["2", "3", "5", "7"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});
只会将“5”和 7“添加到 Firestore 中的数组。这也适用于对象数组。
关于javascript - Firestore - 将数组传递给 arrayUnion(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53252265/