1

My database looks like:

db-dev
   user-metadata
       portfolios
            meta
               pending
                       userId
                              events-collection
                                           doc1
                                           doc2
                                           ...
                       userId2
                              events-collection
                                           doc1
                                           doc2
                                           ...
               verified
                       userId
                              events-collection
                                           doc1
                                           doc2
                                           ...
                       userId2
                              events-collection
                                           doc1
                                           doc2
                                           ...
                       

I want to fetch all docs that belong to verified events-collection. Following this answer and this Blog Post I tried to do the following:

export const getVerifiedEvents = async (eventType: DocumentType) => {
  //eventType is either pending or verified
  const rootRef = Server.db.collection(
    `${constDocumentRefs.merchants_meta_loc}/${eventType}`
  );

  const documents = await const documents = await Server.db
    .collectionGroup('events-collection')
    .orderBy(documentId())
    .startAt(rootRef.path)
    .endAt(rootRef.path + '\uf8ff')
    .get();
  return documents.docs.map((doc) => doc.data());
};

But the above code throws me the following error:

TypeError: Cannot read properties of undefined (reading 'documentId')
  13 |   const documents = await Server.db
  14 |     .collectionGroup('events-collection')
> 15 |     .orderBy(firestore.documentId())
     |             ^
  16 |     .startAt(rootRef.path)
  17 |     .endAt(rootRef.path + '\uf8ff')
  18 |     .get();

Here's how my Server instance looks like:

import { auth, db, storage } from './firebase_server';

const firebaseServer = {
  auth,
  db,
  storage,
};

export default firebaseServer;

Where auth,db and storage are imported from following files:

import admin from 'firebase-admin';

const keyString = process.env.FB_ADMIN_PRIVATE_KEY ?? '{"privateKey": ""}';

const { privateKey } = JSON.parse(keyString);

if (privateKey === '') {
  console.log('FIREBASE_PRIVATE_KEY is not set');
}

if (admin.apps.length === 0)
  admin.initializeApp({
    credential: admin.credential.cert({
      clientEmail: process.env.FB_ADMIN_CLIENT_EMAIL,
      privateKey: privateKey,
      projectId: process.env.FB_ADMIN_PROJECT_ID,
    }),
  });

const db = admin.firestore();
const auth = admin.auth();
const storage = admin.storage();

export { auth, db, storage };

To be precise, I am using firebase-admin ["firebase-admin": "^10.0.1"] npm package and this is just not working whereas in lots of places they have been directly using documentId() with ease. What's the correct grammar for the same?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62

1 Answers1

1

The term "undefined" refers to a variable that has been declared but not given a value. Only objects can have properties and functions in JavaScript. Because undefined is not an object type, executing a method or setting a property on it results in a TypeError: Undefined property cannot be read.

The path could be a single field name (pointing to a document's top level field) or a list of field names (referring to a nested field in the document).

The internal variable id in JavaScript just returns id. Example: .orderBy(firebase.firestore.FieldPath.documentId())

The example that is in the blog you are following is presented like:

.orderBy(FieldPath.documentId())

  • And where do you get `FIeldPath` in second case? – Shivam Sahil Feb 10 '22 at 10:31
  • 1
    The FieldPath is explained in the blog next to point 2: ‘Since we know that there are some limitations when it comes to how much data we store in a single document, according to the official documentation regarding usage and limits’. The shared documentation for usage and limitations also includes information on [Collections, documents, and fields](https://firebase.google.com/docs/firestore/quotas#collections_documents_and_fields): ‘May be passed as a string when all field names in the path are simple, otherwise must be passed as a FieldPath object (e.g. JavaScript FieldPath)’. – Jose German Perez Sanchez Feb 17 '22 at 16:31
  • Please let me know if this comment answers your last comment. – Jose German Perez Sanchez Feb 17 '22 at 16:34