Sending changed Firebase Real-time-database contents to a MQTT device

I'm a total beginner in making node-red custom nodes, so I'm stuck at what I'm doing here which seems pretty simple..

I've already made a custom node which logs into the Firebase database using firebase admin SDK authentication. What I'm trying to do is to send a string to a light bulb(ESP8266 programmed to interact with MQTT) through a default node-red MQTT node in the flow. The problem is, the node must send the string as a msg.payload automatically only if the data in the database has been changed.

I couldn't figure out how I could send the string automatically without the event 'input' emitted so the current code looks like this(This isn't the entire code so If someone requests me, I will post the rest here) :

 var db= admin.database();
        var ref = db.ref(refPath);

        ref.on("value", function(snapshot){
            savedData = snapshot.val();
            //from here, user's own configuration
            console.log(savedData.lightState);
            node.on('input' ,function(msg){
                if(savedData.lightState){
                    msg.payload = "{\"chipid\":\"f5ac43\",\"power\":1}";
                }
                else msg.payload = "{\"chipid\":\"f5ac43\",\"power\":0}";
                node.send(msg);
            });
        },
        function(errorObject){
            savedData = errorObject.code;
        }
        );

        node.on('input', function(msg){
            msg.payload = savedData;
            node.send(savedData);
        });
    }`Preformatted text`

the above works fine, but it only works when I manually attach an inject node next to my custom node. If the data in Firebase database is changed, after pressing the inject node the string with the command is sent to the MQTT node successfully. However, what I want is to force this part

node.on('input' ,function(msg){
** if(savedData.lightState){**
** msg.payload = "{"chipid":"f5ac43","power":1}";**
** }**
** else msg.payload = "{"chipid":"f5ac43","power":0}";**
** node.send(msg);**
** });**

to be triggered without the event 'input' emitted. So I tried to force emit the 'input' event by node.emit('input'). However it throws an exception on the console like below:

8 May 14:13:21 - [red] Uncaught Exception:
8 May 14:13:21 - TypeError: Cannot read property '_msgid' of undefine d
** at fireNode.Node.metric (/usr/lib/node_modules/node-red/node_modu les/@node-red/runtime/lib/nodes/Node.js:532:25)**
** at fireNode.Node.emitInput (/usr/lib/node_modules/node-red/node modules/@node-red/runtime/lib/nodes/Node.js:196:10)**
** at Immediate. (/usr/lib/node_modules/node-red/node_mod ules/@node-red/runtime/lib/nodes/Node.js:179:33)**
** at runCallback (timers.js:705:18)**
** at tryOnImmediate (timers.js:676:5)**
** at processImmediate (timers.js:658:5)**

How could I resolve this problem and successfully send data to MQTT node when the data is changed in Firebase? Thanks all for advance..

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.