Firebase触发onUpdate在更新后返回错误

时间:2018-04-15 08:32:45

标签: firebase firebase-realtime-database google-cloud-functions

更新到最新的Firebase功能“firebase-admin”:“^ 5.12.0”后,  ,“firebase-functions”:“^ 1.0.1和Firebase工具3.18.4,我有以下错误信息,数据库触发器抱怨属性'key'为undefined,如何修复此代码

//这是云功能

exports.OnBalanceChange = functions.database.ref('/balance/{userID}')
  .onUpdate(event => {
    var userid = event.data.key;
    var eventSnapshot = event.data;
    console.log("user ID is", userid);

        return 0;


  });



TypeError: Cannot read property 'key' of undefined
    at exports.OnBalanceChange.functions.database.ref.onUpdate.event (/user_code/index.js:760:28)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /var/tmp/worker/worker.js:700:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1 个答案:

答案 0 :(得分:0)

请参阅适用于Firebase的1.0版Cloud Functions的迁移指南:https://firebase.google.com/docs/functions/beta-v1-diff

具体来说,请参阅this。您将看到onUpdate的单个参数已转换为两个参数,并且参数的类型已更改。您现在可以使用@override Widget build(BuildContext context) { this.context = context; return new Scaffold( key: _scaffoldKey, body: new Stack( children: <Widget>[loginImage(), new Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[new TextField(), new TextField(), new TextField(), new TextField(), new TextField(), new TextField()], )]) ); } (假设第一个arg现在是change.before.key来获取更改文档的ID。