3
// 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?

0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.