Search code examples
node.jsgoogle-cloud-firestorefirebase-admin

How to upgrade to Node.js SDK Admin SDK v10 (modular SDK) getMessaging & getFirestore calls?


I am not finding very detailed explanations how to upgrade to Node.js SDK Admin v10 https://firebase.google.com/docs/admin/migrate-node-v10#use-modules-instead-of-namespaces I try to import messaging as per the docs

import {getFirestore} from "firebase-admin/firestore";
import { getMessaging } from 'firebase-admin/messaging'
const messaging = getMessaging();
const firestore = getFirestore();
import DocumentSnapshot = functions.firestore.DocumentSnapshot;

but then getting all kinds of errors with my old code below. Could someone point to a more in depth example, or help with these error messages?

messaging.MessagingDevicesResponse -> "Cannot find namespace 'messaging'.ts(2503)"
functions.Change<DocumentSnapshot<User>> > "Type 'DocumentSnapshot' is not generic.ts(2315)"
/**
 * Aggregate valid ratings every time a new rating is added.
 * @param {functions.Change} rawChange The change event from the database.
 * @return {Promise<void>} Promise that resolves when the function is done.
 */
export const notifyUsers = async (
    rawChange: functions.Change<functions.firestore.QueryDocumentSnapshot>
): Promise<void | messaging.MessagingDevicesResponse> =>
  lookForAskChangeAndNotifyNearby(
    rawChange as functions.Change<DocumentSnapshot<User>>
  );

Solution

  • To get started have a look at the official documentation on Upgrade to Node.js SDK Admin SDK v10 (modular SDK)
    The Firebase Client SDK still has a compact version that lets you use the older name-spaced syntax but that'll be removed eventually so it's recommended to use the new Modular API.
    The Admin SDK similarly lets you use both name-spaced and Modular syntax; however it's not totally modular yet.
    You may look at the similar example here.