Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase.database.ref path syntax in Cloud Functions and Nodejs

// cloud functions
exports.listenOnWrite = functions.database.ref('/foo/{id}').onWrite(event => {
    console.log("did match....");
})

matches for example: /foo/123123 and /foo/jk3489

// node.js
function startLitener() {
  firebase.database().ref("/foo/{id}").on('child_added', function (postSnapshot) {
    console.log("did match....");
  });
};

does not match anything

1) question

I understand the very comfortable {} syntax in the ref path works only for cloud functions. Or did I miss something?

2) question

How would I formulate ref('/foo/{id}') on a nodejs server?

like image 572
stackovermat Avatar asked Dec 12 '25 03:12

stackovermat


1 Answers

In the declaration of your Cloud Function, you're using the syntax of the Cloud Functions for Firebase library. That's the module that interprets the {} syntax in the path.

The code inside your Node.js program is using the Firebase Admin SDK. So the paths there are interpreted by a different module and unfortunately use a different syntax:

firebase.database().ref("/foo/").on('child_added', function (postSnapshot) {
  var id = postSnapshot.key;
});

Since you're listening for the child_added event, your code gets executed with a snapshot of the new child. When you then call snapshot.key you get the key of that child.

like image 94
Frank van Puffelen Avatar answered Dec 13 '25 21:12

Frank van Puffelen