From 94862c5a6e22133c84e0403f19068268b452cdc6 Mon Sep 17 00:00:00 2001 From: Silas Parker Date: Tue, 21 Apr 2020 13:51:32 +0100 Subject: [PATCH] Handle errors parsing JSON payload, close client when ended, don't log every notification --- pglisten.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pglisten.js b/pglisten.js index c4a59f8..96e095f 100644 --- a/pglisten.js +++ b/pglisten.js @@ -25,7 +25,7 @@ module.exports = function(RED) { this.channel = n.channel; console.log(this.channel); var node = this; - var clientdb = null; + node.clientdb = null; this.status({fill:"red",shape:"ring",text:"disconnected"}); if(this.postgresConfig) { @@ -42,9 +42,9 @@ module.exports = function(RED) { if (node.postgresConfig.db) { config.database = node.postgresConfig.db; } config.ssl = node.postgresConfig.ssl; } - clientdb = new pg.Client(config); + node.clientdb = new pg.Client(config); - clientdb.connect(function(err) { + node.clientdb.connect(function(err) { try { if(err) { @@ -53,14 +53,18 @@ module.exports = function(RED) { } else { console.log("Connected"); node.status({fill:"green",shape:"dot",text:"connected"}); - clientdb.on('notification', function(msg) { - console.log("Notification received"); - msg.payload = JSON.parse(msg.payload); - node.log(JSON.stringify(msg)); - node.send(msg); + node.clientdb.on('notification', function(msg) { + try { + //console.log("Notification received"); + msg.payload = JSON.parse(msg.payload); + //node.log(JSON.stringify(msg)); + node.send(msg); + } catch (error) { + node.error(error); + } }); var query = "LISTEN " + node.channel; - clientdb.query(query); + node.clientdb.query(query); console.log("Listening to :" + node.channel); } } catch (error) {