我正在尝试开发一个云函数,它获取一个金额的值,然后在用新的金额重写总金额之前,将总金额添加到该总金额上。
到目前为止,我得到的是:
exports.newRequest = functions.database.ref("/aRef/{aUID}/{bUID}").onCreate((snapshot, context) =>{
var requestSnapshot = snapshot.data;
console.log('Testing stuff', context.params.aUID);
var UID= context.params.aUID;
var thisAmount= requestSnapshot.child('amount').val();
var totalAmount= functions.database.child('aRef').child('bRef').child(UID).child('totalAmount').val();
var finalAmount = totalAmount+ thisAmount;
return functions.database().ref(`aRef/bRef/${UID}/totalAmount`).set(finalAmount);
});这就是我得到的错误:
TypeError: Cannot read property 'Id' of undefined
at exports.newRequest.functions.database.ref.onWrite.context (/user_code/index.js:14:44)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:114:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:144:20)
at /var/tmp/worker/worker.js:827:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)我知道这个错误将我带到第14行,在本例中,它试图获得{Id}的值,但不知道如何纠正它?
发布于 2019-03-21 06:09:26
你的函数定义是错误的。它应该接受第一个参数和第二个参数,第一个参数是快照,第二个参数是上下文。您现在所拥有的看起来像是试图将这两个对象组合成一个名为context的参数。
您可能希望回到the documentation并了解数据库onWrite触发器是如何工作的。
发布于 2019-03-22 15:01:37
因此,经过一些挖掘和Doug Stevenson的引导,我错误地调用/尝试从上下文而不是快照获取数据,并试图从我的数据库的另一个区域获取数据,而不是放入.once().then()来检索快照。我也不需要onWrite方法,而需要onCreate方法。
下面的最终代码适用于任何需要它的人。
exports.onRequest = functions.database.ref("/Donations/{UID}").onCreate((snapshot, context) =>{
var requestSnapshot = snapshot.val();
var ID = snapshot.child('toid').val();
var thisAmount = snapshot.child('amount').val();
var database = admin.database().ref(`/users/charity/${ID}/acc/totalAmount`);
return database.once('value').then (snapshot =>{
var totalAmount = snapshot.val();
var finalAmount = totalAmount + thisAmount;
console.log('status', snapshot.child('amount').val());
return database.set(finalAmount);
});
});https://stackoverflow.com/questions/55270804
复制相似问题