'getFirebaseAdmin from next-firebase-auth causing TypeScript error
I'm using the following package next-firebase-auth and I'm calling getFirebaseAdmin from next-firebase-auth and when I try to use it I get the following error when I try to use db as a reference in doc(db).
Argument of type 'Firestore' is not assignable to parameter of type 'DocumentReference'. Type 'Firestore' is missing the following properties from type 'DocumentReference': converter, type, firestore, id, and 3 more.
API
import { doc, getDoc } from "firebase/firestore";
import { getFirebaseAdmin } from "next-firebase-auth";
import type { NextApiRequest, NextApiResponse } from "next";
export const getUserInfo = (req: NextApiRequest, res: NextApiResponse) => {
const db = getFirebaseAdmin().firestore();
const docRef = doc(db, "user", "SF");
return res.status(200).json({ data: "hello" });
};
Do I need to use init?
Solution 1:[1]
You can use the db instance to retrieve the doc directly with db.collection("user").doc("SF").
You also have to call init before using getFirebaseAdmin. From the next-firebase-auth documentation:
Initializes
next-firebase-auth, taking a config object. Must be called before calling any other method.
import { doc, getDoc } from "firebase/firestore";
import { init, getFirebaseAdmin } from "next-firebase-auth";
import type { NextApiRequest, NextApiResponse } from "next";
init({/* your init config here */});
export const getUserInfo = (req: NextApiRequest, res: NextApiResponse) => {
const db = getFirebaseAdmin().firestore();
const docRef = db.collection("user").doc("SF");
return res.status(200).json({ data: "hello" });
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
