我目前正在尝试学习 Firebase 函数。 这个新功能似乎非常强大且有用。
我想使用一个函数来捕获数据库写入事件,与数据库中的特定位置进行比较,然后将一些数据(取决于比较结果)写入数据库中的另一个位置。
这是我当前的 Node.js 代码:
exports.verificaFineLavoro = functions.database.ref('/Aziende/{nomeazienda}/LogAsegno/{pushidbracciante}/{pushidlog}/Asegno')
.onWrite(event => {
const original = event.data.val();
console.log('VerificaFineLavoro', event.params.pushId, original);
const aSegno = original;
console.log('aSegno', aSegno +"");
const FineLavoro = ref.parent.parent.parent.parent.child("Asegno/"+aSegno+"/FineLavoro");
return event.data.ref.child('FL').set(FineLavoro);
});
当前该函数已被触发,但由于引用可能错误而停止工作。
最佳答案
Cloud Functions for Firebase documentation on triggering from the database包含显示如何访问数据库中其他节点的代码片段。
例如,此代码片段将一个值设置为触发该函数的节点的同级:
return event.data.ref.parent.child('uppercase').set(uppercase);
这是 sample on counting child nodes 中的另一个片段:
exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite(event => { const collectionRef = event.data.ref.parent; const countRef = collectionRef.parent.child('likes_count'); ...
要访问数据库的根,请使用event.data.ref.root
(以触发该函数的用户的权限访问根节点)或event.data。 adminRef.root
(以完全管理权限访问根目录)。
关于javascript - Cloud Functions for Firebase 在数据库事件函数内执行写入操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208062/