【发布时间】: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