Mat*_*out 3 javascript firebase google-cloud-firestore
如何将数组传递给firebase firestore arrayUnion() 函数?
尝试传递数组时出现此错误。
错误
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)
});
Run Code Online (Sandbox Code Playgroud)
我最终在另一个堆栈溢出答案中找到了使用Function.prototype.apply()的答案。
Function.prototype.apply()的示例
let myArray = ["1", "2", "3"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion.apply(this, myArray)
});
Run Code Online (Sandbox Code Playgroud)
ECMAScript 6示例
使用传播参数
let myArray = ["1", "2", "3"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});
Run Code Online (Sandbox Code Playgroud)
使用上述两种方法传递数组时,Firestore只会将新数组元素添加到Firestore数组中尚不存在的元素。
例如,运行上面的内容,然后运行以下内容
let myArray = ["2", "3", "5", "7"];
docRef.update({
test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});
Run Code Online (Sandbox Code Playgroud)
只会在Firestore中的数组上添加“ 5”和“ 7”。这也适用于对象数组。
| 归档时间: |
|
| 查看次数: |
406 次 |
| 最近记录: |