Firebase Realtime-云函数TypeError:无法读取null的属性“ Male”

时间:2018-11-21 01:26:35

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

我已经创建并成功部署了一个函数,该函数将与数据库中的计数器保持同步。计数器表示不同的数据值,并且在成功将Web应用程序提交到数据库时创建新用户时,计数器将递增。我的数据已成功提交到数据库,但是我的云函数在我的日志中返回错误:

enter image description here

“男性”部分是我的表单提交中的选项之一,用户可以从性别选项的下拉列表中进行选择。这使我认为,至少该功能正在接收该值。但是空值是从哪里来的呢?据我所知,所有提交的数据已成功存储在数据库中。

在扩展到表单中的其他字段之前,我目前正在“性别”上测试我的功能。我的云功能:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.onUserCreate = functions.database
.ref('/students/{studentID}/')
.onCreate((snapshot, context) => {
    const userData = snapshot.val();

    const countersRef = admin.database().ref('/counters/');
    return countersRef.transaction(currentData => {
        currentData[userData.gender] = (currentData[userData.gender] || 0) + 1;
        return currentData;
    });

});

供参考,我的数据库具有以下结构: enter image description here

但是我的“男性”计数器仍为零:

enter image description here

编辑

我登录了console.log(countersRef)console.log(userData)console.log(currentData),现在得到以下信息:

第一个返回了很多有关我的数据库的信息。第二个:

{ afterGrad: 'Masters',
  elaborate: '',
  firstName: 'test',
  gender: 'Male',
  gradDate: '2014',
  hometown: 'home',
  lastName: 'user',
  program: 'Undergraduate',
  race: 'American Indian or Alaska Native',
  timeToComplete: '2 years' }

但是现在,我的currentData日志出现此错误:

enter image description here

0 个答案:

没有答案