[go: nahoru, domu]

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

[Firestore] - FieldValue.arrayUnion broken on version 11.2.0 #1954

Closed
abMarlim opened this issue Oct 26, 2022 · 2 comments
Closed

[Firestore] - FieldValue.arrayUnion broken on version 11.2.0 #1954

abMarlim opened this issue Oct 26, 2022 · 2 comments

Comments

@abMarlim
Copy link

[REQUIRED] Step 2: Describe your environment

  • Operating System version: macOS v12.6
  • Firebase SDK version: 11.2.0
  • Firebase Product: Firestore
  • Node.js version: v16.16.0
  • NPM version: 8.11.0

[REQUIRED] Step 3: Describe the problem

After updating firebase-admin from 10.3.0 to use the new COUNT() feature, I can no longer update a DOC with set() and FieldValue.arrayUnion, even though the property was in the Document previously.

Steps to reproduce:

In version 10.3.0 when I use the code below it works as expected.

await firestore.doc(`someCollection/someDocID`).set({
  date_updated: currentDateTime,
  date_updated_timestamp: currentDateTimeTimestamp,
  operations: admin.firestore.FieldValue.arrayUnion({
    type: operationType,
    operation_at: currentDateTime
  }),
  ...payload
}, { merge: true }).catch(e => {
  console.error(e);
  // throw error
});

In version 11.2.0 when I use the same code above, it returns an error, in the console and does not update the Document.

> operations: admin.firestore.FieldValue.arrayUnion({
> ^
>
> TypeError: Cannot read properties of undefined (reading 'arrayUnion')

Relevant Code:

// Initializing Firebase in CloudFunctions

const
  functions = require("firebase-functions"),
  admin = require('firebase-admin');

const IS_EMULATOR = ((typeof process.env.FUNCTIONS_EMULATOR === 'boolean' && process.env.FUNCTIONS_EMULATOR) || process.env.FUNCTIONS_EMULATOR === 'true');

admin.initializeApp();
let firestore = admin.firestore();

if (IS_EMULATOR) {
  firestore.settings({
    host: 'localhost',
    port: '8081',
    ssl: false
  })
}

I don't know if, between versions 10.3.0 and 11.2.0, the way to call the arrayUnion({}) function changed or if I'm doing something wrong, but since nothing was changed in the code, only the version, I understand that maybe the problem be on the new version, any input is welcome :)

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@abMarlim
Copy link
Author

After looking at some other issues that other users were having with Firestore here in the repository and reading this comment from @lahirumaramba, it was enough minor modifications in the way to call the Firestore functions that the problem was solved. The big problem was the switch to modular SDK

To resolve:

1) Initialize Firebase in Cloud Functions

const
   { initializeApp } = require('firebase-admin/app'),
   { getFirestore } = require('firebase-admin/firestore');

const IS_EMULATOR = ((typeof process.env.FUNCTIONS_EMULATOR === 'boolean' && process.env.FUNCTIONS_EMULATOR) || process.env.FUNCTIONS_EMULATOR === 'true');

initializeApp();
let firestore = getFirestore();

if (IS_EMULATOR) {
   firestore.settings({
     host: 'localhost',
     port: '8081',
     ssl: false
   })
}

2) And when using the Firestore with arrayUnion update function

const
     { FieldValue } = require('firebase-admin/firestore');

await firestore.doc(`someCollection/someDocID`).set({
   date_updated: currentDateTime,
   date_updated_timestamp: currentDateTimeTimestamp,
   operations: FieldValue.arrayUnion({
     type: operationType,
     operation_at: currentDateTime
   }),
   ...payload
}, { merge: true }).catch(e => {
   console.error(e);
   // throw error
});

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

2 participants