【问题标题】:Firebase Functions onCreate trigger Error: TypeError: Cannot read property 'match' of undefinedFirebase 函数 onCreate 触发器错误:TypeError:无法读取未定义的属性“匹配”
【发布时间】:2019-02-02 18:03:48
【问题描述】:

尝试使用为数据库触发的 OnCreate 部署函数,但它一直向我返回此错误。这是简单的代码和错误响应:

只是无法弄清楚为什么匹配未定义错误?

模块版本:

nodejs: Version 8.11.3
Function : deployed with node 8 runtime version. 
"firebase": "^5.3.0"
"firebase-admin": "^5.12.1"
"firebase-functions": "^1.1.0"
"typescript": "^2.5.3"

代码:

import functions = require('firebase-functions');
import admin = require('firebase-admin');
exports.functionName = functions.database.ref('/user/{uid}/email').onCreate((snap, context) => {
    console.log(snap.val());
});

问题更新:这是最新和最新的代码。我正在尝试获取新添加的对象的详细信息。

Firebase 数据库中向用户添加了一个新元素时出错:

TypeError: Cannot read property 'match' of undefined
    at resourceToInstanceAndPath (/srv/node_modules/firebase-functions/lib/providers/database.js:154:26)
    at dataConstructor (/srv/node_modules/firebase-functions/lib/providers/database.js:122:38)
    at Object.<anonymous> (/srv/node_modules/firebase-functions/lib/cloud-functions.js:89:32)
    at Generator.next (<anonymous>)
    at /srv/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at new Promise (<anonymous>)
    at __awaiter (/srv/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /worker/worker.js:728:24
    at <anonymous>

【问题讨论】:

  • 以下应该可以,看我的回答:exports.dbCreate = functions.database.ref('/user/{uid}/email').onCreate((snap, context) =&gt; { const createdData = snap.val(); console.log(createdData); });

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


【解决方案1】:

您正在使用 Cloud Functions 的“旧”语法(Firebase SDK for Cloud Functions 的 1.0.0 版之前)和 onCreate((event) =&gt; {}),并且您项目中的 SDK 版本是 1.1.0 版。

您应该将语法更改为

.onCreate((snap, context) => {})

documentation中所述

【讨论】:

  • 嘿@Renaud Tarnec,我确实尝试过使用,但导致同样的错误。
  • 您使用的是什么确切的控制台日志。您能否在您的问题中添加您正在尝试的确切代码和您遇到的错误?
  • 我还有其他功能,似乎工作正常,但这似乎是个问题。每次,都会向 firebase 数据库添加一个新值,该函数会触发,但总是返回相同的错误。
  • 代码的另一部分是否调用了一个名为 match 的属性?
  • 不,除了这个文件之外,什么都没有,这正是我得到的,因为我所做的任何事情都匹配并返回 undefined
【解决方案2】:

我有同样的错误,当我部署时我有这个警告:

⚠ 函数:您必须有一个 firebase-functions 版本,该版本位于 至少 2.0.0。请运行 npm i --save firebase-functions@latest 在 函数文件夹。

在我安装了它工作的最后一个 firebase-functions 版本之后。

所以我的解决方案是:

npm i --save firebase-functions@latest

【讨论】:

    【解决方案3】:

    我也有这个问题,我相信它与在 firebase 函数中使用 node8 有关。至少这是最后一个重大变化。

    【讨论】:

    • 是的,乔治,谢谢。由于 Node 6 和 Node 8,不得不面对一些集成和迁移问题。现在 firebase 允许您指定节点引擎,这很有帮助。
    • 您能否详细说明您必须进行哪些更改才能使其正常工作。我目前正在使用 firebase-functions v2.x 和 nodejs6,但我仍然收到 OP 标题中提到的错误。
    【解决方案4】:

    我遇到了这个问题,发现我的 Cloud Function 触发事件类型(写入)和我对 firebase.database.ref('...').onChange(...) 的调用不匹配。一旦我把电话切换到firebase.database.ref('...').onWrite(...),一切都很好!

    我犯了一个愚蠢的错误。希望这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 2020-08-01
      • 2020-01-02
      • 2023-04-08
      • 2017-11-20
      • 2022-01-05
      • 2022-01-01
      • 1970-01-01
      • 2022-07-06
      相关资源
      最近更新 更多