Grigori
Grigori

Reputation: 45

How to add and remove Firebase db trigger as part of another db trigger

I've got a small firebase app where I'd like to add a change listener (Db Trigger) when another change happens (e.g. particular property has changed it's value to 'listen') and then remove it when that 'trigger value' changes again. Is it possible?

exports.liveLessonPracticeMonitor = functions.database.ref('/some/property/path')
    .onWrite((change, context) => {
        if (!change.before.exists() && change.after.val() === 'listen') {

            // Can I do it here ?

            // start another listener. 
            functions.database.ref('/some/other/property/path')
                .onWrite((otherChange, otherContext) => {
                    // my code
                }
        } else if (change.before.val() === 'listen' && change.after.val() === 'stop') {
            // stop listening what I've subscribed to two lines above

            // How to do that?
        }

        return null
    })

Upvotes: 0

Views: 54

Answers (2)

Dharmaraj
Dharmaraj

Reputation: 50840

I don't think it is possible but if you are trying to do something based on input you get, you can always add if-else statement to do so. And if you just don't want to do anything you can add a console.log('Invalid Input') in the last else part. So the actual trigger is just logging a statement than doing anything else...

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317447

No, it's not possible. Triggers can't be added or removed programmatically. They must be exported from index.js and deployed using the Firebase CLI.

Upvotes: 1

Related Questions