added on nodes for rtdb and firestore andmade nodes pass through msg objects with as little changes as possible

This commit is contained in:
Peter Svensson
2019-07-15 10:27:01 +02:00
parent e9fd502879
commit fefe2f2509
24 changed files with 453 additions and 48 deletions

View File

@ -1,6 +1,5 @@
let oldpath
let msgin
module.exports = function(RED) {
@ -9,20 +8,31 @@ module.exports = function(RED) {
var node = this;
const cb = (res)=>{
console.log('firebase get result '+res)
console.log('firebase rtdb-get result '+res)
//console.dir(res)
let val = res.val()
console.dir(val)
node.send({payload:val})
//console.dir(val)
if(msgin){
msgin.payload = val
node.send(msgin)
} else {
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)
console.log('* rtdb-get setUpListener for path '+path)
if(path){
this.admin.database().ref(path).once('value').then((res)=>{
cb(res)
}, (fail)=>{
console.log('rtdb-get failure ')
console.dir(fail)
})
} else {
console.log('----- rtdb-get got empty path !!')
console.dir(config)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
if(config.cred){
@ -43,11 +53,11 @@ module.exports = function(RED) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
msgin = msg
setUpListener(path)
}
}.bind(this));
setUpListener(this.path)
}
RED.nodes.registerType("rtdb-get", FirebaseAdmin);