1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add env.get to Function node

This commit is contained in:
Nick O'Leary 2018-08-30 22:42:30 +01:00
parent c0e7d6d826
commit 0c5a76b391
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 40 additions and 1 deletions

View File

@ -53,12 +53,14 @@
<p>The Catch node can also be used to handle errors. To invoke a Catch node, <p>The Catch node can also be used to handle errors. To invoke a Catch node,
pass <code>msg</code> as a second argument to <code>node.error</code>:</p> pass <code>msg</code> as a second argument to <code>node.error</code>:</p>
<pre>node.error("Error",msg);</pre> <pre>node.error("Error",msg);</pre>
<h4>Referring Node Information</h4> <h4>Accessing Node Information</h4>
<p>In the function block, id and name of the node can be referenced using the following properties:</p> <p>In the function block, id and name of the node can be referenced using the following properties:</p>
<ul> <ul>
<li><code>node.id</code> - id of the node</li> <li><code>node.id</code> - id of the node</li>
<li><code>node.name</code> - name of the node</li> <li><code>node.name</code> - name of the node</li>
</ul> </ul>
<h4>Using environment variables</h4>
<p>Environment variables can be accessed using <code>env.get("MY_ENV_VAR")</code>.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -156,6 +156,13 @@ module.exports = function(RED) {
return node.context().global.keys.apply(node,arguments); return node.context().global.keys.apply(node,arguments);
} }
}, },
env: {
get: function(envVar) {
// For now, just return the env var. This will eventually
// also return project settings and subflow instance properties
return process.env[envVar]
}
},
setTimeout: function () { setTimeout: function () {
var func = arguments[0]; var func = arguments[0];
var timerId; var timerId;

View File

@ -1249,6 +1249,36 @@ describe('function node', function() {
}); });
}); });
it('should allow accessing env vars', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload = env.get('_TEST_FOO_'); return msg;"},
{id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var count = 0;
delete process.env._TEST_FOO_;
n2.on("input", function(msg) {
try {
if (count === 0) {
msg.should.have.property('payload', undefined);
process.env._TEST_FOO_ = "hello";
count++;
n1.receive({payload:"foo",topic: "bar"});
} else {
msg.should.have.property('payload', "hello");
done();
}
} catch(err) {
done(err);
} finally {
delete process.env._TEST_FOO_;
}
});
n1.receive({payload:"foo",topic: "bar"});
});
});
describe('Logger', function () { describe('Logger', function () {
it('should log an Info Message', function (done) { it('should log an Info Message', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}]; var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}];