Deleting part of message payload using a function from a memory context

Hi I want to save myself a lot of effort writing a code to delete parts of the messages.
There is probably a better way to do that but I rather do it this way.

I have writen a function inside a function node which to my surprise allowed a deleting part of the message payload using delete.

I tried following this post.
Here
While this method appears to be working for parsing and returns values from functions it doesnt appear working for deleting parts of payload.

Does anyone have any ideas?

Foreword: Apologies if you know exactly why (some people just see extra stuff in msg and want it gone for reasons they either cannot explain or because they "dont need them")

So the question I have is - why do you want to delete parts of the msg?

Usually, you would simply consume the parts of the msg you are interested in, ignore the rest, then pass it on.

Perhaps if you explain what you are trying to achieve (and maybe share your flow) I can have a look and see if you are taking the right approach?

Of course I would like to know good aproaches. I also would like to know if the memory context can be used in that manner.

flows (3).json (26.7 KB)

In the OTMR_Digital_States i have 2 functions

global.get('delete_not_needed', 'memoryOnly')();
del_test();

one of them works the other one not

Hi, so, few things...

  1. You have created a function that you save in global
    image
    ... but this will never work as you do not pass the msg in.

  2. You dont pass the msg through to the global function



Assuming this is academic or you REALLY do want to do this, then you need to pass the base object (in this case, the msg object) into the function as a parameter...

global.set('delete_not_needed', function(msg) {
    delete msg.payload.ext;
    return 0;    
}, 'memoryOnly');

however - this is a very limited and very specific function that has "side effects" / not "pure". I would not recommend this approach. A slightly improved version is included in the code below called deleteObjectProperty. This is considered a better approach as it has more re-use value. (however, deletion of a property is so simple it makes no sense to have a helper function at all!)



A better solution

A slightly better way of dealing with this - put all your helper functions into 1 object for easier access. In the below code example, I make an {object} named myFunctions and store the function definitions in that object, THEN store the single object in global...

const myFunctions = {};

myFunctions.decimalToHex = function(d, padding) {
    var hex = Number(d).toString(16);
    padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
    while (hex.length < padding) {
        hex = "0" + hex;
    }
    return hex;
}

myFunctions.delete_not_needed = function (msg) {
    delete msg.payload.ext;
    return 0;
}

/**
 * @example  myFunctions.deleteObjectProperty(msg.payload, "ext")
 */
myFunctions.deleteObjectProperty = function (/** @type {object}*/obj, /** @type {string}*/ propName) {
    if (obj.hasOwnProperty(propName)) {
        delete obj[propName];
    }
}

//Store 'myFunctions' in global for later use
global.set("myFunctions", myFunctions, 'memoryOnly');

Usage

Now you have all helper functions in one object, you simply can do this...

//////////// Example usage ////////////

var myFunctions = global.get("myFunctions","memoryOnly"); //get the helper functions

//call  decimalToHex  from   myFunctions
var hex1 = myFunctions.decimalToHex(2, 2);// ==> "02"   
var hex2 = myFunctions.decimalToHex(5, 2);// ==> "05"

//call  deleteObjectProperty   from   myFunctions
myFunctions.deleteObjectProperty(msg.payload, "ext"); //delete msg.payload.ext property from msg.payload
myFunctions.deleteObjectProperty(msg, "_meta"); //delete msg._meta property from msg

*/

example....

Lastly...

I am assuming you only want 1 property in your final msg.payload - it is far easier to just create a new object and add the property you do want. I demonstrated that in the above screen shot, but for completeness - here is a little how to...

msg.payload = {}; //set payload to a new empty object
msg.payload.OTMR_DIGITAL_STATES = k; //add a property to msg.payload called OTMR_DIGITAL_STATES 

An alternative way is...

//set payload to a new empty with 1 property 
msg.payload = {
    OTMR_DIGITAL_STATES : k
}; 
msg.payload.; //add a property called OTMR_DIGITAL_STATES 

Thank you for very informative post. I really liked that you showed me couple different ways and points I will take to heart. Which software have you used for the comments and arrows?

I use a couple...

Greenshot
ShareX

ShareX is the most advanced (does screen capture and creates GIFs) but greenshot is a bit better to use.

These are both windows (not sure if there is a comparable Linux or Mac offering)

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.