Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update doc with arrayUnion gives error #1776

Closed
5 of 29 tasks
udarts opened this issue Dec 20, 2018 · 9 comments
Closed
5 of 29 tasks

update doc with arrayUnion gives error #1776

udarts opened this issue Dec 20, 2018 · 9 comments
Assignees

Comments

@udarts
Copy link

udarts commented Dec 20, 2018

Issue

I am trying to update a document in firestore, to add users uid to a userhasread array, but using the following code, gives an error:

setAnnounceToRead(userId) {
        let doc = firebase.firestore().collection('announcements').doc(this.state.id);
        doc.update({ "userhasread": firebase.firestore.FieldValue.arrayUnion(userId) });
}

It produces the following error:

undefined is not an object (evaluating '_firebase.default.firestore.FieldValue.arrayUnion')

Both: this.state.id and userId exist (did a console.log on both).


Project Files

iOS

ios/Podfile:

  • [x ] I'm not using Pods
  • [] I'm using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A

Android

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • If known, the version of the platform are you experiencing the issue on:
    • ADD_SOMETHING_HERE e.g. iOS 10 or Android API 28
  • Operating System:
    • MacOS, version: N/A
    • Windows, version: N/A
    • Other, please specify: N/A
  • Build Tools:
    • Xcode 10
    • Android studio (latest version)
  • **React Native version: **
    • 0.57.8
  • **React Native Firebase library version: **
    • 5.1.1
  • Firebase module(s) you're using that has the issue:
    • N/A
    • Authentication
    • Analytics
    • Cloud Firestore
    • Cloud Messaging (FCM)
    • Crashlytics
    • Dynamic Links
    • Functions Callable
    • Invites
    • Instance ID
    • Notifications
    • Performance Monitoring
    • Realtime Database
    • Remote Config
    • Storage
  • Are you using TypeScript?
    • No
    • Yes, version: N/A
  • Are you using Expo, e.g. ExpoKit?
    • No
    • Yes, I've not ejected
    • Yes, but I have ejected to ExpoKit
    • Yes, but I have ejected to vanilla React Native
    • Expo version: N/A

Think react-native-firebase is great? Please consider supporting the project with any of the below:

@udarts
Copy link
Author

udarts commented Dec 27, 2018

Any idea on this issue?

@Salakar
Copy link
Member

Salakar commented Jan 1, 2019

@udarts what does firebase var point to - your code, is this import firebase from 'react-native-firebase'; exactly or are you passing it around through redux or something like that?

@udarts
Copy link
Author

udarts commented Jan 1, 2019

Imported from react-native-firebase

@udarts
Copy link
Author

udarts commented Jan 1, 2019

Oh, sorry missed a small thing, I am loading the react-native-firebase in a separate file and them loading that file in every component I use.

The file looks like the following:

import RNfirebase from 'react-native-firebase';

const instance = RNfirebase.app();

export default instance;

@Salakar
Copy link
Member

Salakar commented Jan 1, 2019

Ok thanks, that'll be your problem then - the statics are only accessible from the module export and not the app() instance; so you'll need to use that:

import rnfirebase from 'react-native-firebase';

// correct
rnfirebase.firestore.FieldValue.arrayUnion(userId);

// incorrect (what you're doing currently)
rnfirebase.app().firestore.FieldValue.arrayUnion(userId);

Hope that helps, will close the issue now.


Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

@Salakar Salakar closed this as completed Jan 1, 2019
@Salakar Salakar self-assigned this Jan 1, 2019
@udarts
Copy link
Author

udarts commented Jan 2, 2019

Thanks for your reply. Although it might be a bit different, this is how it is setup on my end:

I have a file called: firebase.js, inside is the code I posted above:

import RNfirebase from 'react-native-firebase';

const instance = RNfirebase.app();

export default instance;

Then on multiple components I call that firebase.js file like the following:

import firebase from '../firebase';

Then in that way I am calling:

firebase.firestore.FieldValue.arrayUnion(userId)

Am I missing anything?

@udarts
Copy link
Author

udarts commented Jan 7, 2019

@Salakar Any idea on this?

@udarts
Copy link
Author

udarts commented Jan 9, 2019

@Salakar I've made a change and now use import firebase from 'react-native-firebase';.

When I now try to update a doc with the following code:

setAnnounceToRead(userId) {
        let doc = firebase.firestore().collection('announcements').doc(this.state.id);
        doc.update({ "userhasread": firebase.firestore.FieldValue.arrayUnion(userId) });
}

In Firestore it is shown as:

userhasread: [
    0: useruid
]

While I thought it should be:

userhasread: [
   useruid
]

@akhil-ga
Copy link

akhil-ga commented Apr 12, 2019

A similar case like this one #1776 (comment) for me too.

Code

 const updateObj = {
      priorityMarkerId:  firebase.firestore.FieldValue.arrayUnion(uid),
    };

export const updateMyCollection = async (id, updateObj) => {
  try {
    await firebase
      .firestore()
      .collection('myCollection')
      .doc(id)
      .set(updateObj, { merge: true });
    return {
      status: 'success'
    };
  } catch (error) {
    return {
      status: 'error',
      message: error.message
    };
  }
};

Screenshot from firebase database

Screenshot 2019-04-12 at 6 26 52 PM

Console Log of updating object.

Screenshot 2019-04-12 at 6 23 32 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants