简体   繁体   中英

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

How can I remove ref after my function is finished running? 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". 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

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.

To be clear: ref.transactions() 's do not have to be detached, they just run once, ie there is no callback. Same for ref.set() and ref.once() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM