Suhaib
Suhaib

Reputation: 2061

child_added for new items only with Firebase Cloud Functions

I have this code below which works (no syntax or any other errors) except that the output of this code displays all the results under /server/name:

i.e:

We have a new event:
  { des: 'test123', name: 'Test', nice: 'wew' } lol 

Here is the Code in functions/index.js:

exports.sendFollowerNotification = functions.database.ref('/server/name').onWrite(event => {


 admin.database().ref("/server/name").limitToLast(1).on('child_added', function(snapshot) {

 console.log('We have a new event:', snapshot.val());


   });

Here is the DB:

enter image description here

Update 2:

   exports.sendFollowerNotification = functions.database.ref('/server/name/{num}').onWrite(event => {

console.log('We have a new event:', event.data.val(), 'lol');
});

Output in Logs:

Update 3:

enter image description here

And

enter image description here

Upvotes: 0

Views: 230

Answers (1)

Jen Person
Jen Person

Reputation: 7546

Currently, the trigger is attached higher in the path than you want.

Instead,if you're planning on having multiple lists of events to listen to and this is just list 1 of many, use a wildcard:

exports.sendFollowerNotification = functions.database.ref('/server/name/{num}/{notification}').onWrite(event => {
    ...
})

You can choose wildcard names that better fit your specific code.

Upvotes: 2

Related Questions