I customize a node as following:
dmrequest.js
module.exports = function(RED) {
"use strict";
const request = require("request");
function CustomDMRequestNode(config) {
const url = config.url, datasource = config.datasource ? config.datasource : '';
this.on('input', function(msg) {
const options = {
url: `${url}/service/getData/${datasource}`,
body: msg.payload
};
if(config.credentials.user && config.credentials.password) {
options.headers = {
"Authorization": "Basic " + Buffer.from(`${config.credentials.user}:${config.credentials.password}`).toString("base64")
}
}
request.post(options, (error, response, body) => {
this.send({
"response": response,
"body": body,
"error": error
});
});
});
}
RED.nodes.registerType("atune dmrequest", CustomDMRequestNode);
}
dmrequest.html
<script type="text/x-red" data-template-name="atune dmrequest">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-url"><i class="fa fa-globe"></i> <span>Base URL</span></label>
<input id="node-input-url" type="text" placeholder="http://">
</div>
<div class="form-row">
<label for="node-input-datasource"><i class="fa fa-tag"></i> <span>Data Soruce</span></label>
<input type="text" id="node-input-datasource" placeholder="Data Soruce" />
</div>
<div class="form-row">
<input type="checkbox" id="node-input-useAuth" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-useAuth" style="width: 70%;"><span>Use Authentication</span></label>
<div style="margin-left: 20px" class="node-input-useAuth-row hide">
<div class="form-row node-input-basic-row">
<label for="node-input-user"><i class="fa fa-user"></i> <span>User Name</span></label>
<input type="text" id="node-input-user">
</div>
<div class="form-row">
<label for="node-input-password"> <i class="fa fa-lock"></i> <span>Password</span></label>
<input type="password" id="node-input-password">
</div>
</div>
</div>
</script>
<script type="text/x-red" data-help-name="atune dmrequest">
<p>A dmrequest block to retrieve data from DataManager.</p>
<p>The message can provide the payload in <code>msg.payload</code>.</p>
<p>The block requires some infos:</p>
<ul>
<li><code>Base URL</code>: the base url to connect request to DataManager</li>
<li><code>Data Source</code>: the endpoint to the base url</li>
<li><code>Basic Authentication</code>: the basic authentication (if required)</li>
</ul>
<p>The block will make a request to DataManager and forward the response onto next node in the flow.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('atune dmrequest',{
category: 'atune',
color:"#FFA682",
defaults: {
name: {value:""},
datasource: {value: ""},
url: {
value:"",
validate:function(v) {
return (v.trim().length === 0) || (v.indexOf("://") === -1) || (v.trim().indexOf("http") === 0)
}
},
//authType: {value: ""},
senderr: {value: false}
},
credentials: {
user: {type:"text"},
password: {type: "password"}
},
inputs:1,
outputs:1,
label: function() {
return this.name;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-useAuth").on("change", function() {
if ($(this).is(":checked")) {
$(".node-input-useAuth-row").show();
} else {
$(".node-input-useAuth-row").hide();
//$('#node-input-authType').val('');
$('#node-input-user').val('');
$('#node-input-password').val('');
}
});
}
});
</script>
Everytime the node is created, the following error occurs:
TypeError: Cannot read property 'padEnd' of undefined
at Flow.start (C:\Users\tng\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\flows\Flow.js:254:62)
at start (C:\Users\tng\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\flows\index.js:371:33)
From the error trace log, the error was caused by Flow.js line 254 and it indicates that my customized node seems to have no type. But it is not because i can find the node type information inside flow.json
{
"id": "e695cbbf7c54f4ca",
"type": "atune dmrequest",
"z": "a77111eb3de2b228",
"name": "",
"datasource": "",
"url": "",
"senderr": false,
"x": 400,
"y": 120,
"wires": [
[]
]
},
I spent like nearly 2 hours to find the issue inside the code, but it doesn't seem to have any problem. May some one know what causes the error ?