TypeError: db.collection is not a function Firestore

TypeError: db.collection is not a function Firestore

TypeError: db.collection is not a function Firestore

我在尝试从 Firebase Admin SDK 导入 Firestore 时收到此错误。找不到任何有用的东西,或者如果我导入不正确

代码; index.js(主文件)

var admin = require("firebase-admin");
var serviceAccount = require("./firebase/key.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

var db = admin.firestore()

代码; validate.js(导入)

var db = require('../../../index')

如果您刚刚导入了 Admin SDK,则语法已更改为模块化。查看有关 initializing Firestore 的最新文档,其中显示了如何使用新的 getFirestore 顶级函数初始化 Firestore:

const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
...
const db = getFirestore();