gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'child' of undefined

转载 作者:行者123 更新时间:2023-12-03 01:07:12 25 4
gpt4 key购买 nike

我一直在研究这个问题,但似乎找不到答案。

我有一个带有乘客和司机的应用程序。当乘客请求乘车时,它将“历史”子项添加到数据库中。我也有函数,但是当执行此应用程序时,日志会在添加“历史记录”子项之前导致错误。

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();

我的第一个想法是 child 的“距离”不可用。在我的代码中,直到 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();

最佳答案

您正在针对新版本的 Cloud Functions for Firebase SDK 使用旧 API。您需要更新到新的 API。特别是,您需要注意 firebase-functions 1.0 版本中的更改。 All the changes are summarized here.

特别是,see what changed in Database triggers 。您正在使用旧样式的“事件”参数作为数据库触发器的第一个参数:

exports.newRequest =
functions.database.ref('/history/{pushId}').onCreate(event => {
// ...
})

在 1.0 中,它被更改为接受 DataSnapshot和一个EventContext对象:

exports.newRequest =
functions.database.ref('/history/{pushId}').onCreate((snapshot, context) => {
// ...
})

请参阅文档了解更多详细信息。

关于javascript - 类型错误 : Cannot read property 'child' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52342154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com