代码之家  ›  专栏  ›  技术社区  ›  Ahmed Abdelmeged

Firestore未能使用将项数组合并到另一个数组FieldValue.arrayUnion()

  •  1
  • Ahmed Abdelmeged  · 技术社区  · 7 年前

    我有一个云函数,当某个事件发生时调用它。在函数中,我将得到一个字符串数组 let h:string[] = ["foo","bar","baz"] 当我试图像这样更新文档中的数组字段时,会出现类似的情况 names: admin.firestore.FieldValue.arrayUnion(h)

    Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
        at Object.exports.createStatusError (/srv/node_modules/grpc/src/common.js:87:15)
        at Object.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:1188:28)
        at InterceptingListener._callNext (/srv/node_modules/grpc/src/client_interceptors.js:564:42)
        at InterceptingListener.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:614:8)
        at callback (/srv/node_modules/grpc/src/client_interceptors.js:841:24)
    

    但如果我把密码改成这个 names: admin.firestore.FieldValue.arrayUnion("foo","bar","baz") 它工作,但在我的函数,我收到一个数组不是上述格式,所以它失败了

    这是我想要的。我在文档中有一个数组字段。当函数被触发并从中获取数组时。我想把这个数组添加到现有的names数组中

    names : ["test","test2"]

    添加数组后 names : ["test","test2","foo","bar","baz"]

    怎么解决? 我正在使用typescript 3.0.1

    2 回复  |  直到 7 年前
        1
  •  4
  •   Uzer    7 年前

    如果 admin.firestore.FieldValue.arrayUnion("foo","bar","baz") apply

    因此,解决方案是:

    admin.firestore.FieldValue.arrayUnion.apply(null,h)
    
        2
  •  4
  •   David Fernando Chaves Tamayo    6 年前

    arrayUnion函数在定义上有一个数组值,您需要将您的数组添加到此数组值,在javascript或类型上您可以使用…(NameArray)进行联合。

    items : any[] = [1,2,4,5,67,8];
    admin.firestore.FieldValue.arrayUnion(...items);