我一直在研究这个问题,但似乎找不到答案。
exports.newRequest = functions.database.ref('/history/{pushId}').onCreate(event => {
var requestSnapshot = event.data;
var distance = requestSnapshot.child('distance').val();
var price = distance * 0.5;
var pushId = event.params.pushId;
return requestSnapshot.ref.parent.child(pushId).child('price').set(price);
});
TypeError: Cannot read property 'child' of undefined
at exports.newRequest.functions.database.ref.onCreate.event (/user_code/index.js:17:36)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:733:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
索引:17:36
var distance = requestSnapshot.child('distance').val();
我的第一个想法是,孩子的“距离”是不可用的。在我的代码中,在btnRequest完成之前不会添加子“history”。
有什么想法吗?有没有办法改变密码索引.js使用我应用程序中的代码?
升级API后:
npm install firebase-functions@latest --save
npm install firebase-admin@latest --save-exact
npm install -g firebase-tools
exports.newRequest = functions.database.ref('/history/{pushId}').onCreate(snapshot, context => {
var requestSnapshot = snapshot.val();
var distance = requestSnapshot.child('distance').val();
var price = distance * 0.5;
var pushId = snapshot.params.pushId;
return requestSnapshot.ref.parent.child(pushId).child('price').set(price);
});
不确定上述是否正确,但我部署并得到以下响应:
Error: Error occurred while parsing your function triggers.
ReferenceError: snapshot is not defined
at Object.<anonymous> (/Users/lizg/Desktop/Programs/Android/Google-Play/ryyde_functions/functions/index.js:15:75)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at /Users/lizg/.npm-global/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11
at Object.<anonymous> (/Users/lizg/.npm-global/lib/node_modules/firebase-tools/lib/triggerParser.js:75:3)
编辑#2
更新index.js 代码:
exports.newRequest = functions.database.ref('/history/{pushId}').onCreate((snapshot, context) => {
var requestSnapshot = snapshot.val();
var distance = requestSnapshot.child('distance').val();
var price = distance * 0.5;
var pushId = snapshot.params.pushId;
return snapshot.ref.parent.child(pushId).child('price').set(price);
});
在日志中产生错误:
TypeError: requestSnapshot.child is not a function
at exports.newRequest.functions.database.ref.onCreate (/user_code/index.js:17:37)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:733:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)