Skip to content

Type Error - FiedValue undefined in emulator NOT in production #5956

Closed

Description

@TS22082

Environment info

my directory structure is as so:

project

  • app (react typescript)
    • functions (vanilla js)
      • index.js
      • package.json
    • src
    • package.json

package for my host app (react / typescript):

"firebase": "^9.17.0",

package for functions

"firebase-admin": "^11.2.0",
"firebase-functions": "^4.4.0",

firebase-tools: 12.3.1

Platform:: m1 Mac

Test case

Accessing anything from FieldValue creates error as FieldValue is undefined.

[REQUIRED] Steps to reproduce

In production, the problem is not present. When using the emulator, FieldValue is undefined.

Examples where issues persist:

time: admin.firestore.FieldValue.serverTimestamp()

Error: 
Cannot read properties of undefined (reading 'serverTimestamp')
admins: admin.firestore.FieldValue.arrayUnion(uid)

Error: 
Cannot read properties of undefined (reading 'arrayUnion')
console.log(admin.firestore.FieldValue)
undefined

[REQUIRED] Expected behavior

The timestamp should be saved to the object as it does when I deploy.

[REQUIRED] Actual behavior

error:

13:36:20
I
function[us-central1-onCreateLabel]
Beginning execution of "us-central1-onCreateLabel"
13:36:20
I
function[us-central1-onCreateLabel]
{
  "severity": "INFO",
  "message": "error TypeError: Cannot read properties of undefined (reading 'serverTimestamp')\n    at /Users/thomasw.smith/Documents/Bitrip/GitLab/bitrip-webv2/functions/hooks/onCreateLabel.js:68:26\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async runFunction (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)\n    at async runBackground (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:517:5)\n    at async processBackground (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:501:5)\n    at async /Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:689:21"
}
13:36:20
W
function[us-central1-onCreateLabel]
Error: Error at onCreateLabel hook
    at /Users/thomasw.smith/Documents/Bitrip/GitLab/bitrip-webv2/functions/hooks/onCreateLabel.js:82:13
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runFunction (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)
    at async runBackground (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:517:5)
    at async processBackground (/Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:501:5)
    at async /Users/thomasw.smith/.nvm/versions/node/v16.14.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:689:21
13:36:20
W
function[us-central1-onCreateLabel]
Your function was killed because it raised an unhandled error.

Activity

  1. aalej commented on Jun 12, 2023

    @aalej
    Contributor

    Hi @TS22082, thanks for reaching out to us. I would like to reproduce this behavior you’re encountering. Would it be okay if you could provide me with an MCVE I can run locally, or sample code snippets from your functions/index.js file showing how you’re importing the firebase-admin sdk?

  2. added
    Needs: Author Feedback
    on Jun 12, 2023
  3. TS22082 commented on Jun 14, 2023

    @TS22082
    Author

    The issue was caused by the way I was importing.

    This worked:

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

    This did not:

    const admin = require("firebase-admin");
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions