【发布时间】:2019-01-15 05:28:40
【问题描述】:
我正在尝试在我的函数文件夹中初始化 firebase admin sdk,以便我的云函数可以通过管理员读/写访问权限访问我的数据库,但是当我尝试部署我的函数时返回以下错误
错误:解析触发器时出错:找不到模块'/serviceKey.json'
我尝试在函数目录中运行 npm install 但没有帮助。
这是我的 package.json 文件
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"cors": "^2.8.4",
"express": "^4.16.3",
"firebase": "^5.3.1",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2"
},
"private": true
}
这是我的 index.js 文件
{
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
var firebase = require("firebase");
var admin = require("firebase-admin");
require("firebase/auth");
require("firebase/database");
//require("firebase/firestore");
//require("firebase/messaging");
require("firebase/functions");
var serviceAccount = require("/serviceKey.json");
// Initialize the app with a service account, granting admin
privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<database name>.firebaseio.com"
});
const displayqr = express();
const geturl = express();
displayqr.get('/displayqr', (request, response) => {
console.log("response sent");
response.send("testio/qrdisplay.html");
});
exports.displayqr = functions.https.onRequest(displayqr);
exports.geturl = functions.https.onCall((email) => {// get url
function
});
}
【问题讨论】:
-
/serviceKey.json指的是文件系统根目录中的文件。这很可能是一个错误/错字。可能你想要./serviceKey.json? -
如果您想要一个明确的答案,请分享您的项目文件结构,尤其是您的服务帐户文件相对于项目其余部分的位置。没有它,我们只能猜测你打算在这里发生什么。
-
同样作为 Functions 用户,没有理由使用服务帐户 JSON 文件。您应该只使用应用程序默认凭据:
admin.initializeApp(); -
谢谢,为什么点是必要的,解决了它,但我不知道为什么
标签: node.js firebase firebase-realtime-database google-cloud-functions firebase-admin