假设我有一个firebase节点,它是dbref = firebase.ref(' / Transfer_Request / {pushID] /')。
客户端写两个值; from_ID和to_ID到dbref。如何从Firebase Cloud功能获取from_ID和to_ID的各个值?
我的代码:
exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((event) => {
const original = event.data.val();
const from_ID = original.from_ID;
const to_email_ID = original.to_ID;
//search for to_email ID
return admin.database().set("A transfer request was just made");
});
我有两个错误:
1)
TypeError:admin.database(...)。set不是函数 exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:41:25)在Object。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) 在下一个(本地)在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在__awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (内部/过程/ next_tick.js:135:7)
2)
TypeError:无法从'中读取属性' null的 exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:35:25)在Object。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) 在下一个(本地)在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在__awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (内部/过程/ next_tick.js:135:7)
答案 0 :(得分:1)
第一个问题来自于执行以下操作时您错过了Firebase Reference
。
return admin.database().set("A transfer request was just made");
你应该这样做:
admin.database().ref('...the path where you want to write...').set("A transfer request was just made");
有关详细信息,请参阅Reference和Database的文档。
第二个问题来自于Firebase SDK for Cloud Functions 1.0.0版的新版本,因此语法已经改变。请参阅此文档item。
您应该按如下方式修改代码:
exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((change, context) => {
const original = change.after.val();
const from_ID = original.from_ID;
console.log(from_ID);
const to_email_ID = original.to_ID;
console.log(to_email_ID);
return admin.database().ref('...path...').set("A transfer request was just made")
.catch(error => {
console.log(error);
//...
});
});