【发布时间】:2019-02-19 20:49:53
【问题描述】:
我一直在研究这个,但似乎找不到答案。
我有一个包含骑手和司机的应用。当骑手请求搭车时,它会将“历史”子节点添加到数据库中。我也有功能,但是当这个应用程序被执行时,在添加“历史”子项之前,日志会导致错误。
index.js:
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)
index.js:17:36
var distance = requestSnapshot.child('distance').val();
我的第一个想法是孩子“距离”不可用。在我的代码中,在 btnRequest 完成之前不会添加子“历史记录”。
有什么想法吗?有没有办法更改 index.js 中的代码以使用我的应用程序中的代码?
编辑#1
升级 API 后:
npm install firebase-functions@latest --save
npm install firebase-admin@latest --save-exact
npm install -g firebase-tools
我将 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 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)
index.js:17:37 - var distance = requestSnapshot.child('distance').val();
【问题讨论】:
标签: javascript android google-cloud-functions