最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read Firebase Database entry in Firebase Cloud Functions' index.js? - Stack Overflow

programmeradmin0浏览0评论

I am trying to create device to device push notifications for an iOS app using Firebase Cloud functions. I want to trigger an event whenever a new child is created in database at reference '/user-notifications/{notificationRecipientUid}/{challengeId}'. Here is my index.js code:

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

When a new child is added in the database at that location, I get this error, "TypeError: functions.database.ref(...).once is not a function", in Firebase Console's Functions Logs. So there is no 'once' method available on ref like in web api:

firebase.database().ref('...').once('value').then(function(snapshot) { ... });

My question is: How to read an existing database value inside index.js?

I am trying to create device to device push notifications for an iOS app using Firebase Cloud functions. I want to trigger an event whenever a new child is created in database at reference '/user-notifications/{notificationRecipientUid}/{challengeId}'. Here is my index.js code:

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

When a new child is added in the database at that location, I get this error, "TypeError: functions.database.ref(...).once is not a function", in Firebase Console's Functions Logs. So there is no 'once' method available on ref like in web api:

firebase.database().ref('...').once('value').then(function(snapshot) { ... });

My question is: How to read an existing database value inside index.js?

Share Improve this question edited May 1, 2017 at 10:50 KENdi 7,7792 gold badges18 silver badges31 bronze badges asked May 1, 2017 at 10:09 naeemjawaidnaeemjawaid 5143 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Well, the solution is to use admin instead of firebase like so:

admin.database().ref('...').once('value').then(function(snapshot) {
    ...
});
发布评论

评论列表(0)

  1. 暂无评论