// 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With