0

I'm receiving the following error message when trying to update an array in cloud firestore:

TypeError: Cannot read property 'arrayUnion' of undefined

Here is my code along with my firebase config:

import fire from '../config/Firebase';
  
let tweetToUpdate = 'example-value';

fire.firestore().collection("liked-tweets")
  .doc(userID)
  .update({
    tweets: fire.firestore.FieldValue.arrayUnion(tweetToUpdate),
    });

Firebase.js

import firebase from "firebase";

const CONFIG = {
  apiKey: "",
  authDomain: "twitter-clone-c1b85.firebaseapp.com",
  projectId: "twitter-clone-c1b85",
  storageBucket: "twitter-clone-c1b85.appspot.com",
  messagingSenderId: "987719332990",
  appId: "1:987719332990:web:2fde4fb30d70bcf0bb542f",
  measurementId: "G-T347VWPPJM",
};

const fire = firebase.initializeApp(CONFIG);

export default fire;

Going by the error message, it would suggest the 'fire.firestore' object is undefined and therefore does not have a property named 'FieldValue'. However, should my 'fire' not have a 'firestore' object?

Other methods like fire.firestore(), fire.storage() work fine.

I suspect it has something to do with how I'm exporting/importing firebase but I cannot figure out where I'm going wrong.

I've tried some related SO threads e.g Firestore arrayUnion but have not found an answer that works.

Here are the docs I've been following: Update elements in an array

Any help would be greatly appreciated!! Thanks.

kenwilde
  • 121
  • 8
  • It'd be easier if you could post the whole error stack. There's no `arrayUnion` in your code but we could possibly find where the error originated from your code with the whole stack. – MinusFour Jan 06 '21 at 15:59
  • @MinusFour Hey, thanks for your reply. The error stack began on line 8 of my first code block. There is an arrayUnion method there. Sorry if I misunderstood your Q. – kenwilde Jan 06 '21 at 16:03
  • So my guess would then be there's something wrong with the document you are retrieving. Maybe it doesn't exist? – MinusFour Jan 06 '21 at 16:08
  • @MinusFour I managed to find a solution, see below. Thanks for taking a look though! – kenwilde Jan 06 '21 at 16:10

1 Answers1

1

So I managed to get it working by importing the following:

import firebase from 'firebase/app'

Thanks to the following SO thread

Seems when you want to access the 'firestore' object you need to import from 'firebase/app'.

kenwilde
  • 121
  • 8