added path config variable for rtdb-get
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"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.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
color: '#a6bbcf',
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
path: {value:""},
|
||||
cred: {value: "", type: 'firebase-config'}
|
||||
},
|
||||
inputs:1,
|
||||
@ -38,6 +39,11 @@
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</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">
|
||||
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
|
||||
<input type="text" id="node-input-cred" placeholder="Name">
|
||||
|
||||
40
rtdb-get.js
40
rtdb-get.js
@ -3,31 +3,47 @@
|
||||
let oldpath
|
||||
|
||||
module.exports = function(RED) {
|
||||
|
||||
function FirebaseAdmin(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
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){
|
||||
let c = RED.nodes.getNode(config.cred)
|
||||
this.admin = c.admin
|
||||
}
|
||||
|
||||
const cb = (res)=>{
|
||||
console.log('firebase get result '+res)
|
||||
console.dir(res)
|
||||
let val = res.val()
|
||||
console.log('val='+val)
|
||||
node.send({payload:val})
|
||||
//console.log('------------------------------- rtdg-get config')
|
||||
//console.dir(config)
|
||||
this.path = config.path
|
||||
if(this.path){
|
||||
setUpListener(this.path)
|
||||
}
|
||||
|
||||
|
||||
//console.log('configuring rtdb-get to listen for messages')
|
||||
node.on('input', function(msg) {
|
||||
let path = this.path
|
||||
if(msg && msg.payload){
|
||||
const path = msg.payload.path
|
||||
if(oldpath){
|
||||
this.admin.database().ref(oldpath).off('value', cb)
|
||||
}
|
||||
this.admin.database().ref(path).on('value', cb)
|
||||
oldpath = path
|
||||
path = msg.payload.path
|
||||
setUpListener(path)
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
|
||||
@ -20,6 +20,9 @@ module.exports = function(RED) {
|
||||
this.admin.database().ref(path).set(obj).then((res)=>{
|
||||
console.log('firebase set result '+res)
|
||||
console.dir(res)
|
||||
}).catch((err)=>{
|
||||
console.log('------ ERROR ------')
|
||||
console.dir(err)
|
||||
})
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
Reference in New Issue
Block a user