TypeError:调用Firebase Cloud Function时无法读取未定义的属性“ match”

时间:2019-08-29 04:13:35

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

我在index.js文件中定义了一个clound函数,如下所示:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.newMessage = functions.database
  .ref('/privateMessages/{userA}/{userB}/{message}')
  .onWrite((snap, context) => {
    const original = snap.after.val();
    const userA = context.params.userA;
    const userB = context.params.userB;
    const message = context.params.message;
    console.log('debug', userA, userB, original, message);
    return ;
  });

函数开始执行时,总是出现以下错误:

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:825:24
    at <anonymous>

firebase实时数据库的结构如下。如果在现有的userA和userB之间创建了任何新消息,则将创建一个新消息条目。如果是新的聊天会话,则会创建新的userA和newB以及消息。 enter image description here

functions文件夹中的package.json如下:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true,
  "engines": {
    "node": "8"
  }
}

1 个答案:

答案 0 :(得分:2)

您正在使用一个非常老的firebase-functions模块。最新的是3.2.0。您可以看到一段时间前有same bug的报告,但问题已解决。

(您的firebase-admin也很老。)