firebase-admin-node: [Firestore] admin.firestore.Timestamp.now() fails with firebase-admin 11, works in 10.3 (emulator)
[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')
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 15
- Comments: 20 (4 by maintainers)
Commits related to this issue
- Roll back firebase-admin to 10.3.0 due to https://github.com/firebase/firebase-admin-node/issues/1827 — committed to examind-ai/ltijs-firestore by johnnyoshika 2 years ago
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
TypeScript
TypeScript
tsconfig.json
package.json
Also experiencing the same issue, and
firestore.v1.FirestoreAdminClient
is also undefinedSo is this the way of using the new version of the library with typescript? I can update my code accordingly, just want to make sure this is the correct solution or just a temporary fix.
Also, can the library be fixed so the old way isn’t an option in typescript anymore? Right now the error is thrown only at runtime but the types think it’s okay as is (which it obviously isn’t).
Ah.
The problem was (is)
eslint-plugin-import
😢This will throw lint errors:
whereas this will not (which I only tried because I copy/pasted your code):
The “solution” is to not use the plugin anymore. But I would like to…
Do you have any idea what that’s about?
Failing eslintrc.js:
Passing eslintrc.js:
I rolled back to
v10.3.0
andadmin.firestore.Timestamp.now()
works again.