简体   繁体   中英

Unable to use firebase documentId() in node js admin sdk (firebase-admin npm package grammar issue)

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?

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM