[英]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.