简体   繁体   English

在Firebase函数中,如何在使用后清理数据库引用?

[英]In Firebase functions, how to clean up database refs after use?

How can I remove ref after my function is finished running? 函数运行完毕后,如何删除ref Is it necessary? 有必要吗? I want my function to run as quickly as possible, and don't want "things" piling up. 我希望我的功能尽快运行,并且不希望堆积“东西”。

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.myFunction = functions.database.ref('/path/{uid}').onWrite(event => {
   const ref = event.data.adminRef.root.child('something').child(event.params.uid);

   return ref.transaction(current => {
      if (event.data.exists() && !event.data.previous.exists()) {
         return _.toInteger(current) + _.toInteger(_.get(data, 'value', 0));
      }
   }).then(() => {
      return null; // Avoid "Error serializing return value: TypeError: Converting circular structure to JSON"
    });
 });

A DatabaseReference is nothing you can "remove". 您不能“删除” DatabaseReference It is just a pointer to a location in your database. 它只是指向数据库中某个位置的指针。 The documentation has a page for it: https://firebase.google.com/docs/reference/admin/node/admin.database.Reference 该文档有相应的页面: https : //firebase.google.com/docs/reference/admin/node/admin.database。参考

The only thing you can remove/detach is a callback you set with ref.on(...) , with ref.off(...) , but there is no callback in your code and I think that ref.once() should get the job done most of the time in Functions. 唯一可以删除/分离的是使用ref.on(...)ref.off(...)设置的回调,但是代码中没有回调,我认为ref.once()大多数时候应该在函数中完成工作。

To be clear: ref.transactions() 's do not have to be detached, they just run once, ie there is no callback. 需要明确的是: ref.transactions()不必分离,它们只运行一次,即没有回调。 Same for ref.set() and ref.once() . ref.set()ref.once()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 Firebase database() on() 时如何正确清理函数? - How to correctly clean up function when fetching Firebase database() on()? 如何清除Firebase Cloud Functions中的临时文件 - How to clean temporary files in Firebase Cloud Functions 每次 Karma 测试后如何清理? - How to clean up after each Karma test? 使用后如何从 GPU 清理和卸载 WebGL 画布上下文? - How do I clean up and unload a WebGL canvas context from GPU after use? Express.js:如何在 Firebase 函数中使用带有正则表达式路径模式的 app.use() 时获得“干净路径”? - Express.js: How to get 'Clean Path' when using app.use() with RegEx path pattern in Firebase Functions? 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? 如何在模板中使用$ refs-Vue.js - How to use $refs in template - vuejs Firebase函数如何在特定时间后从实时数据库中删除节点并从存储中删除文件 - Firebase Functions How to remove node from realtime database and file from storage after certain time 如何使用Firebase云功能查询数据库 - How to query database with firebase cloud functions 如何为 firebase 功能选择不同的数据库? - How to choose a different database for firebase functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM