[英]How to access admin firestore using firebase admin SDK?
I am working on an application, where it uses Next.js and Firebase.我正在开发一个应用程序,它使用 Next.js 和 Firebase。
I need to implement server-side authentication.我需要实施服务器端身份验证。 Firebase allows connecting the server using Firebase Admin SDK. Firebase 允许使用 Firebase Admin SDK 连接服务器。
const admin = require("firebase-admin");
const serviceAccount = require("../../../../3-firebase/service-account-key/service-account-file.json");
try {
admin.initializeApp({
credential: admin.credential.cert({
client_email: process.env.FIREBASE_CLIENT_EMAIL,
private_key: process.env.FIREBASE_PRIVATE_KEY,
project_id: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
}),
});
console.log("Initialized.");
} catch (error) {
if (!/already exists/u.test(error.message)) {
console.error("Firebase admin initialization error", error.stack);
}
}
const db = admin.firestore();
export { db };
I installed the firebase-admin package using NPM and I setup firebase admin SDK using the above code by creating a separate file called "firebase-admin.js"我使用 NPM 安装了 firebase-admin package 并通过创建一个名为“firebase-admin.js”的单独文件使用上述代码设置了 firebase admin SDK
import { initializeApp } from "firebase/app";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
const defaultApp = initializeApp(firebaseConfig);
export default defaultApp;
The above code is the default firebase application setup in a separate file called "firebase.js"上面的代码是一个名为“firebase.js”的单独文件中的默认 firebase 应用程序设置
The problem I encountered is I am not able to access the admin firestore.我遇到的问题是我无法访问管理员 firestore。 However, I can able to access the default firestore.但是,我可以访问默认的 firestore。
What I observed is admin SDK is able to initialize using the credentials (private key).我观察到的是管理员 SDK 能够使用凭据(私钥)进行初始化。 But I don't know why I can't access admin firestore.但是我不知道为什么我无法访问admin firestore。 I mean when i use admin firestore the console gives error of 500 (internal server error)我的意思是当我使用 admin firestore 时,控制台给出错误 500(内部服务器错误)
Here is the error message, when I try to use admin.firestore()这是错误消息,当我尝试使用 admin.firestore()
{"error":{"code":"invalid-argument","name":"FirebaseError"}} {“错误”:{“代码”:“无效参数”,“名称”:“FirebaseError”}}
versions I am using "firebase": "^9.6.6", "firebase-admin": "^10.0.2",我使用的版本 "firebase": "^9.6.6", "firebase-admin": "^10.0.2",
Timely help is much needed非常需要及时的帮助
You're importing the initializeApp method from the Firebase Javascript SDK. You should be importing from the Firebase Admin SDK.您正在从 Firebase Javascript SDK 导入 initializeApp 方法。您应该从 Firebase Admin SDK 导入。
import { initializeApp } from "firebase/app";
vs.对比
import { initializeApp } from "firebase-admin/app";
Using firebase-admin v10.0.2
I was able to successfully access the db adding this in my config.js
file, similar to your firebase.js
file.使用firebase-admin v10.0.2
我能够成功访问数据库,将其添加到我的config.js
文件中,类似于您的firebase.js
文件。
const ServiceAccount = require('../superSecretServiceKeyFile.json');
const app = initializeApp(ServiceAccount);
const { getFirestore } = require('firebase-admin/firestore');
const db = getFirestore(app);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.