I am new to Node red.
I need to create Custom Node that has msg.payload value of all node properties combined ( in json format)
Thanks in advance.
I am new to Node red.
I need to create Custom Node that has msg.payload value of all node properties combined ( in json format)
Thanks in advance.
Moved to #general as it’s not about a node that has been written
What have you tried? Where are you stuck? What node properties do you mean?
Also clarify what you mean by Custom Node
I am sorry. I am not very familiar with terminologies here.
I am creating a node
Properties I am referring to :
I want the values input as msg.payload
This is my index.html file :
<script type="text/javascript">
RED.nodes.registerType('AgFarm Farm',{
category: 'Procurement',
color: '#f5ed7a',
defaults: {
farmername: {value:""}
},
inputs:1,
outputs:1,
icon: "farm.png",
label: function() {
return this.farmername||"AgFarm Farm";
}
});
</script>
<script type="text/x-red" data-template-name="AgFarm Farm">
<div class="form-row">
<label for="node-input-agfarmid"><i class="icon-tag"></i> AgFarm ID</label>
<input type="text" id="node-input-agfarmid" placeholder="AgFarm ID">
</div>
<div class="form-row">
<label for="node-input-farmername"><i class="icon-tag"></i> Farmer's Name</label>
<input type="text" id="node-input-farmername" placeholder="Farmer's Name">
</div>
<div class="form-row">
<label for="node-input-village"><i class="icon-tag"></i> Village</label>
<input type="text" id="node-input-village" placeholder="Village">
</div>
<div class="form-row">
<label for="node-input-district"><i class="icon-tag"></i> District </label>
<input type="text" id="node-input-district" placeholder="District">
</div>
<div class="form-row">
<label for="node-input-state"><i class="icon-tag"></i> State</label>
<input type="text" id="node-input-state" placeholder="State">
</div>
<div class="form-row">
<label for="node-input-country"><i class="icon-tag"></i> Country</label>
<input type="text" id="node-input-country" placeholder="Country">
</div>
<div class="form-row">
<label for="node-input-rmlot"><i class="icon-tag"></i> RM Lot</label>
<input type="text" id="node-input-rmlot" placeholder="RM Lot NUmber">
</div>
</script>
<script type="text/x-red" data-help-name="swani node">
<p>A node that increments every time a new message is received and sends Hello World in return.<br/>
</p>
</script>
What should I place in outMsg variable
module.exports = function(RED) {
function swaninode(config) {
RED.nodes.createNode(this,config);
var context = this.context();
var node = this;
this.on('input', function(msg) {
var outMsg = {payload: ""};
node.send(outMsg);
});
}
RED.nodes.registerType("AgFarm Farm",swaninode);
};
At the moment, you only have farmername
listed in the defaults
object of the html file. That means that is the only property the editor will save with your node. So step one is to add all the other properties you care about to that object.
Once you have done that, then the config
object passed to your custom node's constructor function will contain them all as properties.
You can then copy them onto the payload of the message you receive.
In your current code, you are ignoring the msg object passed to your node and sending on a new object. That is a bad practice as it will lose any custom properties the user may have already placed on the message.
this.on('input', function(msg) {
msg.payload = {
farmername: config.farmername,
anotherProperty: config.anotherProperty
}
node.send(msg);
})
Thanks a lot . It worked.