How to make a created node's property accept dynamic (msg) values

I created a node that takes a property called tagId

image

The node works fine when I set tagId to a static value.

I want the node to be able to receive either a static value or a value from msg. Something like

image

Is there a way for me to do this? Do I have to change something in my code?

For reference, here is some of my code:
-------.html-------

<script type="text/javascript">
    RED.nodes.registerType('rename-tag',{
        category: '----',
        color: '#6aacda',
        defaults: {
            name: {value:""},
            tagId: {value:0},
            newName: {value:""}
        },
        inputs:1,
        outputs:1,
        paletteLabel: "Rename Tag",
        icon: "icon.png",
        label: function() {
            return this.name||"Rename Tag";
        }
    });
</script>

-------.js-------

module.exports = function(RED) {
    function RenameTagNode(config) {
        RED.nodes.createNode(this,config);
        this.tagId = config.tagId;
        this.newName = config.newName;
        var node = this;
        node.on('input', function(msg, send, done) {
            send = send || function() { node.send.apply(node,arguments) }
            ...
            if (done) {
                done();
            }
        });
    }
    RED.nodes.registerType("rename-tag",RenameTagNode);
}

Where tagId is the property in question.

The recommended way is to allow the data to look for and pick up the appropriate msg property if the Editor entry is left blank.

Something like (untested):

// ...
node.on('input', function(msg, send, done) {
    if ( node.tagId === '' && msg.payload && msg.payload.foo ) node.tagId = msg.payload.fo
    // ...
1 Like

The "usual way" to do this is if left blank then it can be set by something - like msg.tagID... in which case you would just test for that in the on input code... (with the property set by the user having priority)
The newer more flexible way is to maybe make it a typeInput TypedInput Widget : Node-RED - which can then be set to a string (str) or message property (msg) or other type as well - and gives more flexibility of the property that you would accept. For example the base64 node - node-red-nodes/70-base64.html at master · node-red/node-red-nodes · GitHub and Lines 40-43 and the .js file lines 7 and 12 etc...
Up to you

1 Like

Ok so I implemented the second sugestion (TypedInput Widget) and I just wanted to leave some extra information and useful links for people with less experience (such as myself) who are trying to implement this.

This link has a minimal example of the TypedInput Widget's use.

To understand the .js part of this example and how the input is treated differently depending on the selected type, the documentation for the evaluateNodeProperty function of the RED.util utility helped me a lot as the example provided above does not use the msg type specifically, which needs an extra parameter in the evaluateNodeProperty function (the msg object itself).

@dceejay Thank you for the very fast and concise response which helped achieve exactly what I wanted to do.

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