Warm tip: This article is reproduced from stackoverflow.com, please click
firebase firebase-admin firebase-realtime-database typescript

Firebase Admin with Database in TypeScript

发布于 2020-03-31 22:57:05

I am using Firebase's Admin SDK. It also offers to use the Firebase Database as admin.

The type definition for the created Firebase Admin App looks as follows:

import * as firebaseAdmin from 'firebase-admin';

export type FirebaseContext = {
  firebaseAdmin: firebaseAdmin.app.App;
};

So far, it worked fine. However, if I am using Firebase's TIMESTAMP feature, it throws a type error:

firebaseAdmin.database.ServerValue.TIMESTAMP

// Property 'ServerValue' does not exist on type '(url?: string | undefined) => Database'.ts(2339)

It should be said that the firebaseAdmin instance comes as argument from a function and is thus defined as FirebaseContext.firebaseAdmin from earlier.

So, did I do anything wrong with the earlier type definition? Because, if I don't pass the firebase instance via the function's arguments but import it directly from its module, it doesn't complain about the type definition. So there must be something wrong with the type FirebaseContext.

Thanks for your time and help.

Questioner
Robin Wieruch
Viewed
81
Frank van Puffelen 2020-01-31 22:53

As far as I can see you are now trying to get the database.ServerValue.TIMESTAMP object from a FirebaseApp instance. But the object is actually defined on the global firebase, and not on an app instance. So you will have to explicitly import firebase/admin and call database.ServerValue.TIMESTAMP on that, i.e. firebase.database.ServerValue.TIMESTAMP or admin.database.ServerValue.TIMESTAMP.