【问题标题】:Firestore Array Union: INVALID_ARGUMENT: Cannot convert an array value in an array valueFirestore 数组联合:INVALID_ARGUMENT:无法将数组值转换为数组值
【发布时间】:2020-03-29 19:37:10
【问题描述】:

我正在编写一个执行数组联合操作的 Firebase 云函数:

const addedMemberIds = ['sarah', 'simon', 'lena'];
doc.ref.update({
  memberIds: FieldValue.arrayUnion(addedMemberIds)
});

执行更新导致错误:

INVALID_ARGUMENT: Cannot convert an array value in an array value.

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    arrayUnion 方法的签名如下所示:firebaseFirestore.FieldValue.arrayUnion(...elements: any[])。这意味着它需要一个 参数列表 应该进入数组的元素。但是当您提供一个数组时,该数组被解释为一个参数,这将导致以下内容:['old-user', ['sarah', 'simon', 'lena']]。这当然不是我们想要的,它显然会导致异常,因为 Firestore 不支持将数组作为另一个数组的元素。

    因此,解决方案只是将convertaddedMemberIds 数组添加到一个参数列表,如下所示:

    const addedMemberIds = ['sarah', 'simon', 'lena'];
    doc.ref.update({
      memberIds: FieldValue.arrayUnion(...addedMemberIds)
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2019-07-14
      • 2019-08-11
      • 2020-08-15
      相关资源
      最近更新 更多