Firestore-将数组传递给arrayUnion() [英] Firestore - Pass Array To arrayUnion()

查看:54
本文介绍了Firestore-将数组传递给arrayUnion()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数组传递到Firebase Firestore arrayUnion() 功能?

How do you pass an array to the firebase firestore arrayUnion() function?

尝试传递数组时出现此错误.

I am getting this error when I try to pass an array.

错误

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)
});

推荐答案

我最终找到了使用堆栈溢出答案.

I eventually found the answer of using Function.prototype.apply() in another stack overflow answer.

Function.prototype.apply()的示例

let myArray = ["1", "2", "3"];

docRef.update({
    test: firebase.firestore.FieldValue.arrayUnion.apply(this, myArray)
});

ECMAScript 6示例

使用 spread参数

let myArray = ["1", "2", "3"];

docRef.update({
    test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});

使用上述任何一种方法传递数组时,Firestore只会将新数组元素添加到Firestore数组中尚不存在的新元素.

When passing an array using either of the above methods, Firestore will only add new array elements to that do not already exist in the Firestore array.

例如,运行上面的代码,然后运行以下的代码

For example, running the above, then running the following

let myArray = ["2", "3", "5", "7"];

docRef.update({
    test: firebase.firestore.FieldValue.arrayUnion(...myArray)
});

只会在Firestore中的数组上添加"5"和"7".这也适用于对象数组.

would only add "5" and 7" to the array in Firestore. This works for an array of objects as well.

这篇关于Firestore-将数组传递给arrayUnion()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆