dont error is property in does not exist

This commit is contained in:
Steve-Mcl 2024-04-25 18:14:08 +01:00
parent 5951632693
commit e45d14f3f4
7 changed files with 39 additions and 13 deletions

View File

@ -16,6 +16,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
const { getMessagePropertySafe } = require('../utils.js')(RED)
function RangeNode(n) { function RangeNode(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.action = n.action; this.action = n.action;
@ -29,7 +30,7 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on('input', function (msg, send, done) { this.on('input', function (msg, send, done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = getMessagePropertySafe(msg, node.property);
if (value !== undefined) { if (value !== undefined) {
var n = Number(value); var n = Number(value);
if (!isNaN(n)) { if (!isNaN(n)) {

View File

@ -29,7 +29,7 @@ module.exports = async function(RED) {
var querystring = require("querystring"); var querystring = require("querystring");
var cookie = require("cookie"); var cookie = require("cookie");
var hashSum = require("hash-sum"); var hashSum = require("hash-sum");
const { getMessagePropertySafe } = require('../utils.js')(RED)
// Cache a reference to the existing https.request function // Cache a reference to the existing https.request function
// so we can compare later to see if an old agent-base instance // so we can compare later to see if an old agent-base instance
@ -433,8 +433,7 @@ in your Node-RED user directory (${RED.settings.userDir}).
} }
} }
var payload = null; var payload = null;
const value = RED.util.getMessageProperty(msg, node.property) const value = getMessagePropertySafe(msg, node.property)
if (method !== 'GET' && method !== 'HEAD' && typeof value !== "undefined") { if (method !== 'GET' && method !== 'HEAD' && typeof value !== "undefined") {
if (opts.headers['content-type'] == 'multipart/form-data' && typeof value === "object") { if (opts.headers['content-type'] == 'multipart/form-data' && typeof value === "object") {

View File

@ -15,9 +15,11 @@
**/ **/
module.exports = function(RED) { module.exports = function(RED) {
const csv = require('./lib/csv')
"use strict"; "use strict";
const csv = require('./lib/csv')
const { getMessagePropertySafe } = require('../utils.js')(RED)
function CSVNode(n) { function CSVNode(n) {
RED.nodes.createNode(this,n) RED.nodes.createNode(this,n)
const node = this const node = this
@ -69,8 +71,8 @@ module.exports = function(RED) {
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
node.hdrSent = false; node.hdrSent = false;
} }
if (msg.hasOwnProperty(node.property)) { let inputData = getMessagePropertySafe(msg, node.property)
let inputData = RED.util.getMessageProperty(msg, node.property) if (typeof inputData !== "undefined") {
if (typeof inputData == "object") { // convert object to CSV string if (typeof inputData == "object") { // convert object to CSV string
try { try {
if (!(notemplate && (msg.hasOwnProperty("parts") && msg.parts.hasOwnProperty("index") && msg.parts.index > 0))) { if (!(notemplate && (msg.hasOwnProperty("parts") && msg.parts.hasOwnProperty("index") && msg.parts.index > 0))) {
@ -416,8 +418,8 @@ module.exports = function(RED) {
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
node.hdrSent = false node.hdrSent = false
} }
if (msg.hasOwnProperty(node.property)) { let inputData = getMessagePropertySafe(msg, node.property)
let inputData = RED.util.getMessageProperty(msg, node.property) if (typeof inputData !== "undefined") {
if (typeof inputData == "object") { // convert object to CSV string if (typeof inputData == "object") { // convert object to CSV string
try { try {
// first determine the payload kind. Array or objects? Array of primitives? Array of arrays? Just an object? // first determine the payload kind. Array or objects? Array of primitives? Array of arrays? Just an object?

View File

@ -19,6 +19,7 @@ module.exports = function(RED) {
const Ajv = require('ajv'); const Ajv = require('ajv');
const ajv = new Ajv({allErrors: true}); const ajv = new Ajv({allErrors: true});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
const { getMessagePropertySafe } = require('../utils.js')(RED)
function JSONNode(n) { function JSONNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -48,7 +49,7 @@ module.exports = function(RED) {
} }
validate = true; validate = true;
} }
var value = RED.util.getMessageProperty(msg,node.property); var value = getMessagePropertySafe(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
if (typeof value === "string" || Buffer.isBuffer(value)) { if (typeof value === "string" || Buffer.isBuffer(value)) {
// if (Buffer.isBuffer(value) && node.action !== "obj") { // if (Buffer.isBuffer(value) && node.action !== "obj") {

View File

@ -3,6 +3,7 @@ module.exports = function(RED) {
"use strict"; "use strict";
var xml2js = require('xml2js'); var xml2js = require('xml2js');
var parseString = xml2js.parseString; var parseString = xml2js.parseString;
const { getMessagePropertySafe } = require('../utils.js')(RED)
function XMLNode(n) { function XMLNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -12,7 +13,7 @@ module.exports = function(RED) {
this.propertyOut = n.propertyOut||this.property; this.propertyOut = n.propertyOut||this.property;
var node = this; var node = this;
this.on("input", function(msg,send,done) { this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = getMessagePropertySafe(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
var options; var options;
if (typeof value === "object") { if (typeof value === "object") {

View File

@ -2,13 +2,14 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var yaml = require('js-yaml'); var yaml = require('js-yaml');
const { getMessagePropertySafe } = require('../utils.js')(RED)
function YAMLNode(n) { function YAMLNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload"; this.property = n.property||"payload";
this.propertyOut = n.propertyOut||this.property; this.propertyOut = n.propertyOut||this.property;
var node = this; var node = this;
this.on("input", function(msg,send,done) { this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = getMessagePropertySafe(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
if (typeof value === "string") { if (typeof value === "string") {
try { try {

View File

@ -0,0 +1,21 @@
function utils(RED) {
/**
* Returns the value of a property in a message object using a path. If not found, returns undefined.
* @param {Object} msg - The message object.
* @param {string} path - The path to the property.
*/
function getMessagePropertySafe (msg, path) {
try {
return RED.util.getMessageProperty(msg, path)
} catch (_e) {
return undefined
}
}
return {
getMessagePropertySafe
}
}
module.exports = utils