kumail
kumail

Reputation: 1409

firebase.firestore.FieldValue.arrayUnion is not a function

I'm trying to update an array in my firestore, i followed the documentation provided by Google (https://firebase.google.com/docs/firestore/manage-data/add-data) but It doesn't work, I also checked to make sure I have the latest version of the firebase npm module.

Here's my code:

> db
                    .collection('Data')
                    .doc('One')
                    .collection('Doc')
                    .doc(this.$route.params.id.toLowerCase())
                    .update({
                        myArr: firebase.firestore.FieldValue.arrayUnion(
                           'test'
                        ),
                    })
                    .then(() => console.log('Successfully written'))
                    .catch(err => console.log(err));

Upvotes: 13

Views: 11883

Answers (5)

Armar
Armar

Reputation: 289

If your using the new modular version 9 for javascript use the code below

import { getFirestore, arrayUnion, updateDoc } from "firebase/firestore";
const db = getFirestore(config);    
const pathRef = doc(db, "doc", "id");
    await updateDoc(pathRef , {
        myArr: arrayUnion("test")
    });

Upvotes: 5

kebsama
kebsama

Reputation: 61

From this issue

This has been released as part of @google-cloud/firestore v0.16.0. It is not yet available via Firebase Admin, but will be released shortly. Note that the function name is admin.firestore.FieldValue.arrayUnion().

Upvotes: 5

Gabriel Guita
Gabriel Guita

Reputation: 21

import firebase from 'firebase/app'

const arrayToUpdate = firebase.firestore.FieldValue.arrayUnion(value)

Upvotes: 2

Broda Noel
Broda Noel

Reputation: 1960

"firebase-admin": "^6.0.0", is the version where arrayUnion was added. Upgrade the npm package.

Check https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/CHANGELOG.md#060

Upvotes: 1

kumail
kumail

Reputation: 1409

Firebase npm module was out of date. Had to manually reinstall

Upvotes: 5

Related Questions