Save variable to object type

In node-red, how to save a variable to an object structure, not a string. Such as a JSON object

Hi @limengsong

Can you give an example of what you mean?


as the picture shows:
There are two properties for the workCenterCode and workCenterName name.
I want to use an attribute instead, such as workcenter:{code:"xxx",name:"xxx"}
Save as an object instead of a string.
The front-end component selects the select component.

How are you generating that object today?

There are lots of ways to structure a message object however you want it. The Function node with some JavaScript, the Change node can do it.

I'm not going to list out all the different ways it can be done.

If you can show us how you are building that object today, we can show you how to modify it to get what you want.

    <div class="form-row">
        <label for="node-input-workcenterCode" data-i18n="workcenter.name" ><i class="icon-tag"></i> </label>
        <select type="text" id="node-input-workcenterCode" style="width:70%;">
        </select>
    </div>

        oneditprepare: function () {
            var node = this;
            $.ajax({
                url: `/api/workCenters`,
                dataType: 'json',
                type: 'GET',
                success: (data) => {
                    if (data.data && data.data.length > 0) {
                        $('#node-input-workcenterName').val(`${data.data[0].code}`)
                        for (let v of data.data) {
                            if (v.code !== node.workcenterCode) {
                                $("#node-input-workcenterCode").append(
                                    `<option value=${v.code}>${v.name}</option>`);
                            } else {
                                $("#node-input-workcenterCode").append(
                                    `<option value=${v.code} selected = "selected" >${v.name}</option>`
                                );
                            }
                          
                        }
                    }
                },
                error: (data) => {
                    //error
                }
            });

        },

Thanks!

Oh, you are talking about node properties. I thought you were asking about message structure.

You can use oneditsave you set node properties to whatever structure you want.

The Change node, for example, does just that for the list of rules you set in the node. Have a look at its html.

@knolleary

        oneditsave: function () {
            this.workcenter=[{code:"111",name:"222"}];
        },


like this?

Thanks