我正在尝试使用 Firebase 云功能为 iOS 应用程序创 build 备到设备的推送通知。每当在引用 '/user-notifications/{notificationRecipientUid}/{challengeId}' 的数据库中创建新 child 时,我想触发一个事件。这是我的 index.js 代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendWinLoseOrTieNotification = functions.database.ref('/user-notifications/{notificationRecipientUid}/{challengeId}').onWrite(event => {
...
const challengeId = event.params.challengeId;
functions.database.ref('/challenges/' + challengeId).once('value').then(function(snapshot) {
const challengerUid = snapshot.val().challengerUid;
...
});
});
当在该位置的数据库中添加新子项时,我在 Firebase 控制台的函数日志中收到此错误“TypeError:functions.database.ref(...).once 不是一个函数”。所以没有可用的“一次”方法 在 ref 上,就像在 web api 中一样:
firebase.database().ref('...').once('value').then(函数(快照) {
...
});
我的问题是:如何读取 index.js 中的现有数据库值?
最佳答案
好吧,解决方案是像这样使用 admin 而不是 firebase:
admin.database().ref('...').once('value').then(function(snapshot) {
...
});
关于javascript - 如何读取 Firebase Cloud Functions 的 index.js 中的 Firebase 数据库条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43717434/