added path config variable for rtdb-get

This commit is contained in:
Peter Svensson
2019-06-23 17:29:38 +02:00
parent 56b47400bd
commit 632e06c586
4 changed files with 38 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-contrib-firebase-admin", "name": "node-red-contrib-firebase-admin",
"version": "1.0.2", "version": "1.0.3",
"description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -5,6 +5,7 @@
color: '#a6bbcf', color: '#a6bbcf',
defaults: { defaults: {
name: {value:""}, name: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'} cred: {value: "", type: 'firebase-config'}
}, },
inputs:1, inputs:1,
@ -38,6 +39,11 @@
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label> <label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name"> <input type="text" id="node-input-cred" placeholder="Name">

View File

@ -3,31 +3,47 @@
let oldpath let oldpath
module.exports = function(RED) { module.exports = function(RED) {
function FirebaseAdmin(config) { function FirebaseAdmin(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
var node = this; var node = this;
const cb = (res)=>{
//console.log('firebase get result '+res)
//console.dir(res)
let val = res.val()
//console.log('val='+val)
node.send({payload:val})
}
let setUpListener = (path)=>{
//console.log('rtdb-get setUpListener for path '+path)
if(oldpath){
this.admin.database().ref(oldpath).off('value', cb)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
if(config.cred){ if(config.cred){
let c = RED.nodes.getNode(config.cred) let c = RED.nodes.getNode(config.cred)
this.admin = c.admin this.admin = c.admin
} }
const cb = (res)=>{ //console.log('------------------------------- rtdg-get config')
console.log('firebase get result '+res) //console.dir(config)
console.dir(res) this.path = config.path
let val = res.val() if(this.path){
console.log('val='+val) setUpListener(this.path)
node.send({payload:val})
} }
//console.log('configuring rtdb-get to listen for messages')
node.on('input', function(msg) { node.on('input', function(msg) {
let path = this.path
if(msg && msg.payload){ if(msg && msg.payload){
const path = msg.payload.path path = msg.payload.path
if(oldpath){ setUpListener(path)
this.admin.database().ref(oldpath).off('value', cb)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
} }
}.bind(this)); }.bind(this));

View File

@ -20,6 +20,9 @@ module.exports = function(RED) {
this.admin.database().ref(path).set(obj).then((res)=>{ this.admin.database().ref(path).set(obj).then((res)=>{
console.log('firebase set result '+res) console.log('firebase set result '+res)
console.dir(res) console.dir(res)
}).catch((err)=>{
console.log('------ ERROR ------')
console.dir(err)
}) })
} }
}.bind(this)); }.bind(this));