The attempt to combine an array of elements with another array using FieldValue.arrayUnion() in Firestore was unsuccessful

My cloud function is triggered when a specific event occurs. Within the function, I receive an array of strings like this example:

let h:string[] = ["foo","bar","baz"]
. When I attempt to update an array field within my document using
names: admin.firestore.FieldValue.arrayUnion(h)
, it fails and throws the following error in the console:

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)

However, if I change the code to:

names: admin.firestore.FieldValue.arrayUnion("foo","bar","baz")
, it works. But since my function receives an array, not the above format, it fails.

What I need is to have an array field in the document called 'names'. When the function is triggered and I receive the array from it, I want to add this array to the existing names array.

Before adding the array: names : ["test","test2"]

After adding the array:

names : ["test","test2","foo","bar","baz"]

How can I fix this issue? I am using typescript 3.0.1

Answer №1

To utilize the arrayUnion function, simply initialize an array with the values you wish to add. In JavaScript or similar languages, you can use ...(NameArray) in order to merge arrays.

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

Answer №2

When using

admin.firestore.FieldValue.arrayUnion("foo","bar","baz")
, we have the option to utilize a function known as apply . This function can be found as an object member on every function and is incredibly valuable. Apply allows for passing a single array of arguments to the function (instead of comma separated arguments) and executing it.

To implement this solution, follow this code:

admin.firestore.FieldValue.arrayUnion.apply(null,h)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When attempting to add an object to an array in a collection, there was an issue with

In my application, I am accessing a collection in Mongodb using ReactJS and mongoose. One of the properties in this collection is set to an Array type. When I add elements to this Array, they are successfully added but for some reason, the _id field that ...

Utilizing Vue and TypeScript - Retrieving a Variable Declared in the Data() Method within Another Method

I recently made the switch to using Vue3 with TypeScript after previously working with Vue2 and JavaScript. I am encountering an issue in my IDE where it is showing an error (even though the code itself functions correctly, but may not be entirely accurate ...

Tips for passing an array to a different function using Jquery

I need to pass an array to another function <div id="test"></div> <div id="test2"></div> <input type="button" value="chk" id="go" /> <script> $(function() { var c = 1; var i = 5; var dat ...

Incorporating an additional ion-item alongside the existing one instead of substituting it

I am retrieving a list of questions from an API with pagination. I have a button that triggers a function to load the next page of questions. Instead of replacing the previous page, I want to append the new questions below the existing ones. Here is my cur ...

What is the process for importing a package in AtmosphereJS?

Despite following the steps outlined in this guide for using atmosphere packages in my meteor project, I am encountering errors when I run meteor. One specific issue I am facing is with the vsivsi:job-collection package. When I try to reference it like th ...

Generate a multi-level array by organizing data from a table in a way that ensures duplicate IDs are assigned the same

I'm currently working with a table structured like this: --primary id---office id---- |----1-------|---10--------| |----2-------|---10--------| |----3-------|---20--------| |----4-------|---10--------| |----5-------|---20--------| ------------------- ...

The state data is not being properly updated and is getting duplicated

While developing a loop to parse my API data, I encountered an issue where the values obtained were not being captured properly for dynamically loading corresponding components based on their characteristics. The problem arose after implementing useState() ...

Utilizing JavaScript variables imported from an external library in Next.js: A Guide

I am currently working on a Next.js with Typescript website and I am in the process of adding advertisements. The ad provider has given me instructions to embed this JavaScript code on my site: <script src="//m.servedby-buysellads.com/monetization. ...

Trim the final digits from the arrays

I have an array that contains various strings: var arr = ['1234','23C','456','356778', '56'] My goal is to filter out array elements that are less than 3 characters or more than 4 characters. The resultin ...

Using Typescript alongside Angular 1.6 and browserify for your development needs

Currently navigating the world of working with Angular types in TypeScript and bundling code using Browserify. After following a TypeScript guide related to Gulp, I utilized npm to install the Angular types and put together this simple ts file. import * a ...

Creating an object array in Visual Basic: A step-by-step guide

Can an array of objects be created in visual basic? I'm currently working on a battle system and I would like to randomly choose a Monster object from an array when the battle begins. If this is possible, could someone please demonstrate how to stor ...

Sending decimal values between Arduinos is proving to be troublesome

I have a project where I need to transfer three float values from one Arduino to another and store them in an array on the receiving Arduino. However, when I print the received float values, I only see the first two digits in the serial monitor. As a begi ...

Cypress: Importing line in commands.ts is triggering errors

After adding imports to the commands.ts file, running tests results in errors. However, in commands.ts: import 'cypress-localstorage-commands'; /* eslint-disable */ declare namespace Cypress { interface Chainable<Subject = any> { c ...

Determine the position of the highest value in a given array and extract the elements N positions to the left and right of this maximum value

I've been working on some C coding that involves manipulating arrays. My goal is to accomplish the following: Given an input array, create a new_array that contains only the first 10 elements Determine the index of the largest element in the new_arra ...

Best Practices for Updating UI State in Client Components Using NextJS and Server Actions

My goal is to create a page using nextjs 14 that functions as a stock scanner. This page will retrieve data from an external API using default parameters, while also offering users the ability to customize parameters and re-run the scan to display the resu ...

Obtaining JSON data in React Native without prior knowledge of the key

When I receive this type of data, the keys are common but the items vary. How can I extract and add this data to my list of categories? { "99": "Venues", "100": "Party Supplies", "101": "Enter ...

Retrieve information from Firebase's real-time database using a function

I am currently working on a Cloud Functions script to send notifications by retrieving the user ID from the Firebase Realtime Database and sending a notification to a specific device using their token. However, I am facing an issue in extracting the user I ...

Generating Random Numbers in C++ Array

I am working on a C++ program that generates 6 random numbers between 1 and 54. Below is the code I have written. However, when running the program, I encountered an unexpected output like this: Bugunku sansli loto sayiniz: 17 1 12 32 33 3418568 I can ...

Can a constant value be dynamically defined in Typescript?

Trying to dynamically define a constant in Typescript has proven to be more challenging than I anticipated. Here's what I attempted: define(name: string, value: any): boolean { var undef; const name = value; return name == undef; } ...

Next.js: Generating static sites only at runtime due to getStaticProps having no data during the build phase, skipping build time generation

I am looking to customize the application for individual customers, with a separate database for each customer (potentially on-premise). This means that I do not have access to any data during the build phase, such as in a CI/CD process, which I could use ...