如何在云功能中获取firebase数据库数据?

时间:2018-03-12 06:29:50

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

我正在使用firebase数据库来存储我的iOS应用数据。

enter image description here

我正在此应用中保存用户跟踪数据,该工作正常。

我必须使用推送令牌向用户发送推送通知(userID = 57411405),我将保存在' IOS'字段。

我正在使用的云功能:

此云功能正常运行。我能够跟踪保存新跟踪数据的事件。以下是此云功能的日志:

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

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.databasechanges = functions.database.ref('/users/{id}/LocationTracking').onWrite(event =>{

var eventSnapshot = event.data

console.log('UserId - ', event.params.id);

const userID = event.params.id
const root = event.data.ref.root

admin.database().ref('users/{id}/NotificationToken').on('value').then((snapshot) => {

  var token = snapshot.val().IOS;

  console.log('token',token)

  return snapshot.val();
}).catch(error => {
  console.error(error);
  res.error(500);
});

return eventSnapshot.val()

});

但是在云功能控制台上我收到了这个错误:

enter image description here 现在,我无法弄清楚如何访问此推送令牌(IOS)并使用云功能发送推送通知。

1 个答案:

答案 0 :(得分:3)

要获得字段" IOS",请尝试:

//inside the trigger function
admin.database().ref('/users/'+event.params.id+'/NotificationToken/IOS').once('v‌​alue').then((snapshot) => { 
var token=snapshot.val().IOS;

 });