【问题标题】:Unable to use firebase documentId() in node js admin sdk (firebase-admin npm package grammar issue)无法在节点 js admin sdk 中使用 firebase documentId()(firebase-admin npm 包语法问题)
【发布时间】:2022-02-10 07:12:59
【问题描述】:

我的数据库看起来像:

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
                                           ...
                       

我想获取属于已验证事件集合的所有文档。在此answer 和此Blog Post 之后,我尝试执行以下操作:

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

但是上面的代码给我抛出了以下错误:

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

这是我的服务器实例的样子:

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

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

export default firebaseServer;

其中 auth、db 和 storage 是从以下文件导入的:

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 };

确切地说,我正在使用firebase-admin ["firebase-admin": "^10.0.1"] npm 包,这只是行不通,而在很多地方他们一直在轻松地直接使用 documentId() .正确的语法是什么?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    术语"undefined" 指的是已声明但未赋予值的变量。 JavaScript 中只有对象可以具有属性和函数。因为 undefined 不是对象类型,所以在其上执行方法或设置属性会导致 TypeError: Undefined property cannot be read。

    路径可以是单个字段名称(指向文档的顶级字段)或字段名称列表(引用文档中的嵌套字段)。

    JavaScript 中的内部变量 id 只是返回 id。 示例:.orderBy(firebase.firestore.FieldPath.documentId())

    您所关注的blog 中的示例如下所示:

    .orderBy(FieldPath.documentId())

    【讨论】:

    • 在第二种情况下你从哪里得到FIeldPath
    • FieldPath 在第 2 点旁边的博客中进行了解释:'由于我们知道在单个文档中存储多少数据时存在一些限制,根据有关使用的官方文档和限制”。有关用法和限制的共享文档还包括有关 Collections, documents, and fields 的信息:“当路径中的所有字段名称都很简单时,可以作为字符串传递,否则必须作为 FieldPath 对象(例如 JavaScript FieldPath)传递”。
    • 如果这条评论回答了您的最后一条评论,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 2021-06-04
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2018-09-02
    相关资源
    最近更新 更多