question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Firestore] admin.firestore.Timestamp.now() fails with firebase-admin 11, works in 10.3 (emulator)

See original GitHub issue

[REQUIRED] Step 2: Describe your environment

  • Operating System version: macOS Monterey 12.4
  • Firebase SDK version: 11.3.0
  • Firebase Product: Firestore
  • Node.js version: v16.14.2
  • NPM version: 8.15.0

tsconfig.json

{
  "compilerOptions": {
    "lib": ["ES2020"],
    "module": "commonjs",
    "target": "ES2020",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "dist",
    "sourceMap": true,
    "strict": true,
    "allowSyntheticDefaultImports": true
  },
  "compileOnSave": true,
  "include": ["src"],
  "exclude": ["node_modules", "**/node_modules/*"]
}

package.json

{
  "name": "functions",
  "scripts": {
    "build": "rm -rf dist && tsc",
    "watch": "rm -rf dist && npx tsc -w --skipLibCheck --noEmitOnError",
    "serve": "firebase emulators:start --import=./.cache --export-on-exit",
    "deploy": "firebase deploy --only hosting",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "dist/index.js",
  "dependencies": {
    "@firebase/app-compat": "^0.1.29",
    "axios": "^0.27.2",
    "cors": "^2.8.5",
    "firebase-admin": "^11.0.0",
    "firebase-functions": "^3.22.0",
    "stripe": "^9.14.0"
  },
  "devDependencies": {
    "@firebase/app-types": "^0.7.0",
    "@types/cors": "^2.8.12",
    "@types/stripe-v3": "^3.1.27",
    "firebase-functions-test": "^2.2.0",
    "jest": "^28.1.3",
    "typescript": "^4.7.4"
  },
  "private": true
}

[REQUIRED] Step 3: Describe the problem

admin.firestore.Timestamp.now() throws TypeError: Cannot read properties of undefined (reading β€˜now’) (~same with admin.firestore.increment(1)).

In emulator, I haven’t dared try it β€œlive”.

Steps to reproduce / Relevant Code:

test.ts

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
export const test = functions.region('europe-west1').https.onRequest(async (request, response) => {
  const res = await admin.firestore().collection('mytable').add({
    createdAt: admin.firestore.Timestamp.now(),
  })
  response.json({
    id: res.id,
  })
})

Result:

TypeError: Cannot read properties of undefined (reading 'now')

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:15
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
lahirumarambacommented, Aug 8, 2022

It seems like as we move on to the modular SDK we should import the types as needed. The following seems to work for me:

Node.js

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

initializeApp();

const f = getFirestore();
const t = Timestamp.now();
const fv = FieldValue.delete();

TypeScript

import { getFirestore, Timestamp, FieldValue, Firestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

initializeApp()

const f: Firestore = getFirestore();
const t: Timestamp = Timestamp.now();
const fv: FieldValue = FieldValue.delete();

TypeScript

import * as admin from 'firebase-admin';

admin.initializeApp();

const f = admin.firestore();
const t = admin.firestore.Timestamp.now();
const fv = admin.firestore.FieldValue.delete();

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "lib": ["es2015"]
}

package.json

{
  "name": "repro",
  "version": "1.0.0",
  "main": "dist/test.js",
  "scripts": {
    "start": "tsc && node dist/test.js"
  },
  "engines": {
    "node": "16"
  },
  "devDependencies": {
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "firebase-admin": "^11.0.1"
  }
}
β”œβ”€β”¬ firebase-admin@11.0.1
β”‚ β”œβ”€β”€ @fastify/busboy@1.1.0
β”‚ β”œβ”€β”€ @firebase/database-compat@0.2.4
β”‚ β”œβ”€β”€ @firebase/database-types@0.9.12
β”‚ β”œβ”€β”€ @google-cloud/firestore@5.0.2
β”‚ β”œβ”€β”€ @google-cloud/storage@6.3.0
β”‚ β”œβ”€β”€ @types/node@18.6.5
β”‚ β”œβ”€β”€ jsonwebtoken@8.5.1
β”‚ β”œβ”€β”€ jwks-rsa@2.1.4
β”‚ β”œβ”€β”€ node-forge@1.3.1
β”‚ └── uuid@8.3.2
└── typescript@4.7.4
2reactions
milo-commented, Aug 3, 2022

Also experiencing the same issue, and firestore.v1.FirestoreAdminClient is also undefined

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timestamp | JavaScript SDK | Node.js (client) API reference
Reference for Timestamp. ... Static now. now ( ) : Timestamp. Creates a new timestamp with the current date, with millisecond precision.
Read more >
firestore.Timestamp import is undefined in firebase-admin@11
In firebase-admin@10 I used to retrieve the document data using this approach: const userRef = await firestore() .collection('users') .
Read more >
firebase - Bountysource
The user.sendEmailVerification() needs to be supported by Firebase Cloud Functions. This is available in client side but not server side. When using CloudΒ ......
Read more >
Firebase Firestore firestore.Timestamp.now() でエラー v11.5.0
import { Timestamp } from 'firebase-admin/firestore' γ‚’θΏ½εŠ γ—γ¦ε‘Όγ³ε‡Ίγ™γ¨ ... Timestamp.now() fails with firebase-admin 11, works in 10.3 ...
Read more >
cannot read properties of undefined (reading 'firestore')
I am using react-admin to fetch data from a Firestore API in the following ... Timestamp.now() fails with firebase-admin 11, works in 10.3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found