我正在使用 firebase 数据库来存储我的 iOS 应用程序数据。
我正在这个运行良好的应用程序中保存用户跟踪数据。
我必须使用保存在“IOS”字段中的推送 token 向用户 (userID = 57411405) 发送推送通知。
我正在使用的这个云函数:
此云功能运行良好。我能够跟踪保存新跟踪数据的事件。这是此云功能的日志:
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()
});
但是在云函数控制台上我收到了这个错误:
最佳答案
要获取字段“IOS”,试试这个:
//inside the trigger function
admin.database().ref('/users/'+event.params.id+'/NotificationToken/IOS').once('value').then((snapshot) => {
var token=snapshot.val().IOS;
});
关于javascript - 如何在云功能中获取 firebase 数据库数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49229286/