尝试从 Node.js Firebase 函数中读取 Firebase 数据库时无法确定 Firebase 数据库 URL

Gra*_*amD 6 node.js firebase firebase-realtime-database google-cloud-functions firebase-admin

我正在使用 Flutter 构建一个访问 Firebase 数据库的应用程序。Flutter 方面一切都很好......但我是 Node.js 和 Cloud Functions 的新手。我正在尝试创建一个 Node.js 函数来对一个 Firebase 数据库节点上的记录删除事件做出反应,然后从其他两个 Firebase 数据库节点和来自 Firestore 的图像文件中删除记录。

我正在通过functions.database.onDelete调用对触发事件做出反应,没问题,但在下一个障碍 ietrying 读取 admin.database 以获取快照时遇到了问题。

我创建了一个虚拟函数,用于.onUpdate获取触发事件(不想像我使用的那样继续重新创建我的数据.onDelete),然后尝试读取我的 Firebase 数据库以访问不同的节点。触发事件被很好地拾取,但我似乎没有数据库引用 Url 来进行读取……但它是同一个数据库。调用 的控制台日志上的输出process.env.FIREBASE_CONFIG显示存在 Url。

包含的函数代码也有注释以显示我在控制台日志上获得的各种输出。

我快疯了.....请谁能告诉我我哪里出错了。过去两天一直在搜索 Google、Stackoverflow、Firebase 文档:-(


const admin = require("firebase-admin"); // Import Admin SDK
const functions = require("firebase-functions"); // Import Cloud Functions

admin.initializeApp({
  credential: admin.credential.cert(
    require("./user-guy-firebase-adminsdk.json")
  )
});

exports.testDeleteFunction = functions.database
  .ref("/user-guys/{userGuyId}")
  // Using .onUpdate as I don't want to keep restoring my data if I use .onDelete
  .onUpdate((snapshot, context) => {

    const userData = snapshot.after.val();
    const userId = userData.userId;

    console.log('userId: ' + userId);         // Gives correct console log output: userId: wajzviEcUvPZEcMUbbkPzcw64MB2

    console.log(process.env.FIREBASE_CONFIG);
    // Gives correct console log output:
    // {"projectId":"user-guy","databaseURL":"https://user-guy.firebaseio.com","storageBucket":"user-guy.appspot.com","cloudResourceLocation":"us-central"}


    // I have tried each of the four following calls and received the console log message as indicated after each.
    // 
    // var root = admin.database.ref();       // Console Log Message: TypeError: admin.database.ref is not a function
    // var root = admin.database().ref();     // Console Log Message: Error: Can't determine Firebase Database URL.
    // var root = admin.database.ref;         // Fails at line 31 below with the message indicated.
    // var root = admin.database().ref;       // Console Log Message: Error: Can't determine Firebase Database URL.

    console.log(root.toString);               // Console Log Message: TypeError: Cannot read property 'toString' of undefined.

    // This is intended to read a chat thread for two users but processing never gets here.
    var database = root.child('chats').child('0cSLt3Sa0FS26QIvOLbin6MFsL43GUPYmmAg9UUlRLnW97jpMCAkEHE3');

    database
      .once("value")
      .then(snapshot => {
        console.log(snapshot.val());
      }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
      });
    return null; // Will do stuff here once working.
  });

Run Code Online (Sandbox Code Playgroud)

代码注释中显示的错误消息。

小智 6

您需要在 admin.initializeApp 中添加数据库网址

 admin.initializeApp({
 databaseURL:"your_database_url"
  });
Run Code Online (Sandbox Code Playgroud)


Dou*_*son 5

如果要使用 FIREBASE_CONFIG 中的配置,则应不带参数初始化 Admin SDK:

admin.initializeApp();
Run Code Online (Sandbox Code Playgroud)

这将为您的项目使用默认服务帐户,该帐户应具有对数据库的完全读写访问权限。

  • 但这不是完整的解决方案,因为初始化脚本中的参数被实际 index.js 中的其他函数使用,例如私钥、身份验证 ID、证书。所以我必须在最终版本中有一个参数。我尝试将 `databaseUrl` 添加到脚本中,但没有帮助,所以需要做更多的研究。关于使用初始化参数运行它的任何想法? (4认同)

小智 5

在firebase中选择实时数据库并复制您的url添加到fire配置中的设置中或观看此视频https://www.youtube.com/watch?v=oOm_9y3vb80

配置示例

const config = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  databaseURL: "https://youUrl.firebaseio.com/",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};
Run Code Online (Sandbox Code Playgroud)