Node.js Firebase函数:无法读取属性';用户id';未定义的

Node.js Firebase函数:无法读取属性';用户id';未定义的,node.js,firebase,firebase-realtime-database,firebase-cloud-messaging,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,Google Cloud Functions,我正在尝试用我的移动应用程序执行一个简单的hello world firebase功能,我想记录用户ID,这样我就可以看到该功能是否正常工作。 这是我当前的javascript代码: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotif

我正在尝试用我的移动应用程序执行一个简单的hello world firebase功能,我想记录用户ID,这样我就可以看到该功能是否正常工作。 这是我当前的javascript代码:

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

exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => {

  console.log('Testing stuff', event.params.user_id);

  return;
});
当新数据写入特定databasetable时,会触发此错误,但会显示此错误:

TypeError: Cannot read property 'user_id' of undefined
    at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:8:44)
    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)
TypeError:无法读取未定义的属性“user\u id”
在exports.sendNotification.functions.database.ref.onWrite(/user\u code/index.js:8:44)

在对象处。

您需要安装最新的firebase功能和firebase管理员:

npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools
要使用新的API,请查看此处以了解更多信息:

更改此选项:

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

exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => {

console.log('Testing stuff', event.params.user_id);
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((change, context) => {

console.log('Testing stuff', context.params.user_id);
进入这个:

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

exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => {

console.log('Testing stuff', event.params.user_id);
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((change, context) => {

console.log('Testing stuff', context.params.user_id);
对于
onWrite
onUpdate
事件,数据参数具有
字段。其中每一个都是一个
DataSnapshot
,其方法与中提供的方法相同


params

一个对象,包含为实时数据库触发器的ref()方法提供的path参数中的通配符值

更多信息请点击此处:


请注意:您可能正在使用某个教程的旧版本,该版本尚未更新,无法使用Firebase API 1.0版的云函数API。请通知教程的创建者,他们的内容已过期。