@babymotte/node-red-worterbuch
Version:
Nodes for Wörterbuch integration
36 lines (30 loc) • 890 B
JavaScript
const { setupWb } = require("../utils");
module.exports = function (RED) {
function WorterbuchGetNode(config) {
const node = this;
const wb = setupWb(node, RED, config, (status) =>
node.send([null, null, status])
);
node.on("input", (msg, send, done) =>
onInput(RED, config, node, msg, wb, send, done)
);
}
RED.nodes.registerType("worterbuch-get", WorterbuchGetNode);
};
function onInput(RED, config, node, msg, wb, send, done) {
let key =
RED.util.evaluateNodeProperty(config.key, config.keyType, node, msg) ||
msg.topic;
wb.connection
.get(key)
.then((val) => {
const newMsg = { ...msg, topic: key, payload: val };
send([[newMsg], null, null]);
done();
})
.catch((err) => {
const newMsg = { ...msg, topic: key, payload: err.cause };
send([null, [newMsg], null]);
done();
});
}