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

View File

@ -29,7 +29,7 @@ module.exports = async function(RED) {
var querystring = require("querystring");
var cookie = require("cookie");
var hashSum = require("hash-sum");
const { getMessagePropertySafe } = require('../utils.js')(RED)
// Cache a reference to the existing https.request function
// 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;
const value = RED.util.getMessageProperty(msg, node.property)
const value = getMessagePropertySafe(msg, node.property)
if (method !== 'GET' && method !== 'HEAD' && typeof value !== "undefined") {
if (opts.headers['content-type'] == 'multipart/form-data' && typeof value === "object") {

View File

@ -15,9 +15,11 @@
**/
module.exports = function(RED) {
const csv = require('./lib/csv')
"use strict";
const csv = require('./lib/csv')
const { getMessagePropertySafe } = require('../utils.js')(RED)
function CSVNode(n) {
RED.nodes.createNode(this,n)
const node = this
@ -69,8 +71,8 @@ module.exports = function(RED) {
if (msg.hasOwnProperty("reset")) {
node.hdrSent = false;
}
if (msg.hasOwnProperty(node.property)) {
let inputData = RED.util.getMessageProperty(msg, node.property)
let inputData = getMessagePropertySafe(msg, node.property)
if (typeof inputData !== "undefined") {
if (typeof inputData == "object") { // convert object to CSV string
try {
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")) {
node.hdrSent = false
}
if (msg.hasOwnProperty(node.property)) {
let inputData = RED.util.getMessageProperty(msg, node.property)
let inputData = getMessagePropertySafe(msg, node.property)
if (typeof inputData !== "undefined") {
if (typeof inputData == "object") { // convert object to CSV string
try {
// 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 = new Ajv({allErrors: true});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
const { getMessagePropertySafe } = require('../utils.js')(RED)
function JSONNode(n) {
RED.nodes.createNode(this,n);
@ -48,7 +49,7 @@ module.exports = function(RED) {
}
validate = true;
}
var value = RED.util.getMessageProperty(msg,node.property);
var value = getMessagePropertySafe(msg,node.property);
if (value !== undefined) {
if (typeof value === "string" || Buffer.isBuffer(value)) {
// if (Buffer.isBuffer(value) && node.action !== "obj") {

View File

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

View File

@ -2,13 +2,14 @@
module.exports = function(RED) {
"use strict";
var yaml = require('js-yaml');
const { getMessagePropertySafe } = require('../utils.js')(RED)
function YAMLNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
this.propertyOut = n.propertyOut||this.property;
var node = this;
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 (typeof value === "string") {
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