【问题标题】:TypeError: Cannot read property 'child' of undefinedTypeError:无法读取未定义的属性“孩子”
【发布时间】: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


    【解决方案1】:

    您针对较新版本的 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 中,这已更改为接受 DataSnapshotEventContext 对象:

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

    有关详细信息,请参阅文档。

    【讨论】:

    • 我已按照建议更新了我的 API,并已更改为 exports.newRequest = functions.database.ref('/history/{pushId}').onCreate((snapshot, context) => { 。 ..对于代码的其余部分...我已将“事件”更改为“快照”,但代码中的上下文在哪里发生了变化?
    • 您没有在快照和上下文参数周围加上括号。
    • 我按照你的建议做了(快照,上下文)见 EDIT #2
    • 它似乎在“有点”工作,现在让它做我需要它做的事情(将在另一个问题中发布)非常感谢
    猜你喜欢
    • 2019-01-29
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多