【问题标题】:cannot find service account in firebase functions admin sdk在 Firebase 功能 admin sdk 中找不到服务帐户
【发布时间】: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


【解决方案1】:

你把你的index.js放在同一个文件夹里,然后说

var serviceAccount = require("./myfirebaseapp-firebase-adminsdk-my-secret-key.json");

然后你admin.initializeApp

【讨论】:

  • 谢谢,我错过了初期
  • 很高兴为您提供帮助。点击接受,这样如果你得到答案,问题就不会一直冒泡。
【解决方案2】:

就我而言,我在项目的根目录中有 service-account 文件。它在本地工作,但部署它是一个问题。

../service-account.json  (Root)

什么对我有用:

如果是 Node 应用程序,请将文件从项目的根目录移动到您的函数目录,因为 firebase 会将文件托管在不同的位置

./service-account.json  (Inside functions directory)

【讨论】:

  • 谢谢!您对 Firebase 将根目录和函数存储在不同位置的评论对我很有帮助。
【解决方案3】:

将 json 文件放在 lib 文件夹中对我有用(functions\lib),我认为当我使用 typescript 时,firebase 将从 lib 文件中部署 javascript 函数,因此它会在 lib 文件夹中查找 json 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2020-03-09
    • 2017-10-18
    相关资源
    最近更新 更多