flash
flash

Reputation: 11

Firebase Admin SDK Firestore without Prisma: TypeError: The "payload" argument must be of type object. Received null

I am working on a Next.js project where I integrate Firebase Admin SDK to handle Firestore database operations and used clerk for authentication process. However, I keep encountering the following error when trying to add a document to a Firestore collection:

Error log:

TypeError: The "payload" argument must be of type object. Received null code: 'ERR_INVALID_ARG_TYPE'

Additionally, when accessing the app in the browser, I get:

[ Server ] Error: "length" is outside of buffer bounds

Source: actions\actions.ts (24:41) @ add
  22 | const docCollectionRef = adminDb.collection("documents");
  23 | 
  24 | const docRef = await docCollectionRef.add({
  25 |   title: "New Doc",
  26 | });

Code snippet. Here is how I am initializing Firebase Admin SDK:

import { initializeApp, getApps, App, getApp, cert } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

let app: App;

const serviceKey = require("@/service_key.json");

// Initialize Firebase Admin SDK
if (getApps().length === 0) {
  console.log("Initializing Firebase Admin...");
  app = initializeApp({
    credential: cert(serviceKey),
  });
} else {
  console.log("Reusing existing Firebase Admin instance...");
  app = getApp();
}

// Get Firestore instance
const adminDb = getFirestore(app);

export { app as adminApp, adminDb };

And the function where the error occurs:

import { adminDb } from "@/firebase-admin";
import { auth } from "@clerk/nextjs/server";

export async function createNewDocument() {
  const { sessionClaims } = await auth();

  const docCollectionRef = adminDb.collection("documents");
  const data = { title: "New Doc" };

  if (!data || typeof data !== "object") {
    throw new Error("Invalid payload: Data must be an object");
  }

  const docRef = await docCollectionRef.add(data);

  await adminDb
    .collection("users")
    .doc(sessionClaims?.email!)
    .collection("rooms")
    .doc(docRef.id)
    .set({
      userId: sessionClaims?.email!,
      role: "owner",
      createdAt: new Date(),
      roomId: docRef.id,
    });

  return { docId: docRef.id };
}

I tried these steps:

  1. Verified the service_key.json file by confirming the file exists and contains valid private_key and client_email.

  2. Ensured data passed to docCollectionRef.add(data) is an object.

  3. Validated the environment: Confirmed I’m using Node.js runtime, and the firebase-admin package is up to date (v13.0.2).

Expected: A new document to be created in the documents Firestore collection. No runtime errors during initialization or document creation.

P.S. I am new to Next.js and exploring it. I'd been stuck on it since night. Any help or pointers would be appreciated!

Upvotes: 1

Views: 119

Answers (1)

721 atk
721 atk

Reputation: 1

In my case, I am using firestore from GCP and changed the Json file for the service account to that of the default user of firestore and this error was resolved. The content of the error does not seem to be particularly relevant; it takes time in GCP as it often has nothing to do with the content of the error.

Upvotes: 0

Related Questions