【问题标题】:Firebase web - close connection to the realtime databaseFirebase web - 与实时数据库的关闭连接
【发布时间】:2021-11-08 13:28:04
【问题描述】:

我正在使用带有 Javascript 的 Web 上的 Firebase。
如何关闭与实时数据库的连接

var config = {....};
firebase.initializeApp(config);

// get the firebase DB reference
var db = firebase.database().ref();

//doing something
db.push(....);

// close the firebase connection ???
db.close() || firebase.disconnect() || or something like that...

尝试了很多功能 - goOffline() , close() 等。
但没有什么办法。

任何帮助将不胜感激... tnx 队友

【问题讨论】:

  • 调用goOffline() 将关闭客户端和数据库之间的连接。您是如何得出调用它不会关闭连接的结论的?
  • goOffline 在 firebase 对象或 databaseREF 上?你可以添加一个代码吗?也许我没有正确实施它.. @FrankvanPuffelen

标签: javascript firebase firebase-realtime-database


【解决方案1】:

试试这个-

firebase.initializeApp(config);

// Kill this firebase app.
firebase.app().delete().then(function() {
  firebase.initializeApp(newConfig);
});

goOffline() 将关闭连接,但 Firebase 将继续在本地工作,并且应用程序将保留在内存中。

【讨论】:

  • 如何重新连接兄弟?
【解决方案2】:

尝试使用:

firebaseRef.off();

这将结束您与 firebase 的连接并停止与数据库通信。

【讨论】:

    【解决方案3】:

    对于 JSv9,正如 @Frank van Puffelen 所说,调用 goOffline(db)

    与服务器断开连接(所有数据库操作将离线完成)。

    引用自Official Document

    请注意,这里的dbDatabase,而不是DatabaseRef。 可能你调用的方式不对。

    我为此查找了这个jest 错误:A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.(我做了一个注释here in my knowledgebase。)并在afterAll() 中添加一个goOffline(db) 实际上解决了它。

    【讨论】:

      【解决方案4】:

      对于其他人的信息,这是我在获取所需数据后断开 Firebase 的方式。

      import {
        getDatabase,
        goOnline,
        goOffline,
        get,
        child,
        ref,
      } from 'firebase/database';
      
      // something like this
      const db = getDatabase()
      try {
        goOnline(db);
        const snapshot = await get(child(ref(db), `visitors/${id}`));
        if (snapshot.exists()) {
          const data = snapshot.val();
        } else {
          console.log('No data available');
        }
      } catch (error) {
        console.log('error', error);
      }
      goOffline(db);
      

      【讨论】:

        【解决方案5】:

        似乎您正在尝试使用 CLIENT SIDE conf 执行此操作服务器端。

        您是否正确设置了 firebase.initializeApp({}) 作为服务器的配置?

        查看这里的文档:https://firebase.google.com/docs/database/server/start#server-sdk-authentication

        我们遇到了同样的问题,但是当我们根据文档正确设置所有内容并将其与“以有限权限进行身份验证”一起使用时,所有这些都会消失。

        var firebase = require("firebase");
        
        // Initialize the app with a service account, granting admin privileges
        firebase.initializeApp({
          databaseURL: "https://databaseName.firebaseio.com",
          serviceAccount: "path/to/serviceAccountCredentials.json"
        });
        
        // As an admin, the app has access to read and write all data, regardless of Security Rules
        var db = firebase.database();
        var ref = db.ref("restricted_access/secret_document");
        ref.once("value", function(snapshot) {
          console.log(snapshot.val());
        });
        

        【讨论】:

        • 请注意 - 如何关闭与实时数据库的连接?
        • Firebase 现在已经发布了一个服务器端包-> firebase.google.com/docs/admin/setup 它应该可以解决这个问题。
        猜你喜欢
        • 2022-06-10
        • 1970-01-01
        • 2019-03-05
        • 2018-02-10
        • 2019-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        相关资源
        最近更新 更多