Firebase 函数:无法读取未定义的属性“user_id" [英] Firebase functions: cannot read property 'user_id' of undefined

本文介绍了Firebase 函数:无法读取未定义的属性“user_id"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的移动应用程序执行一个简单的 hello world firebase 功能,我想记录用户 ID,以便我可以看到该功能确实有效.这是我当前的 javascript 代码:

const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp(functions.config().firebase);export.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => {console.log('测试内容', event.params.user_id);返回;});

当新数据写入特定数据库表时会触发,但会出现此错误:

TypeError: 无法读取未定义的属性user_id"在exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:8:44)在对象<匿名>(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在下一个(本地)在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在/var/tmp/worker/worker.js:700:26在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

通知数据库如下所示:

解决方案

您需要安装最新的firebase-functions和firebase-admin:

npm install firebase-functions@latest firebase-admin@latest --savenpm install -g firebase-tools

为了能够使用新的 API,请在此处查看更多信息:

https://firebase.google.com/docs/functions/get-开始#set_up_and_initialize

改变这一点:

const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp(functions.config().firebase);export.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => {console.log('测试内容', event.params.user_id);

进入这个:

const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp();export.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((change, context) => {console.log('测试内容', context.params.user_id);

<块引用>

对于onWriteonUpdate 事件,数据参数有beforeafter 字段.每个都是一个 DataSnapshot,与 admin.database.DataSnapshot

<小时><块引用>

参数

包含路径参数中通配符值的对象,提供给实时数据库触发器的 ref() 方法.

更多信息在这里:

云函数 v1.0 变化

EventContext#params

更改

I am trying to do a simple hello world firebase function with my mobile app, I want to log the user ID so I can see that the function does work. This is my current javascript code:

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;
});

It does trigger when new data is written to specific databasetable but this error shows up:

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)

The notification database looks like this:

解决方案

You need to install the latest firebase-functions and firebase-admin:

npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools

to be able to use the new API, check here for more info:

https://firebase.google.com/docs/functions/get-started#set_up_and_initialize

Change this:

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);

into this:

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);

For onWrite and onUpdate events, the data parameter has before and after fields. Each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot


params

An object containing the values of the wildcards in the path parameter provided to the ref() method for a Realtime Database trigger.

more info here:

Cloud functions v1.0 Changes

EventContext#params

Change

这篇关于Firebase 函数:无法读取未定义的属性“user_id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆