variables set in node.js code are not reflecting in the subsequent apigee policy

Hi,

I am using node.js to fetch keys from a vault and I need to use those keys in the subsequent policies.

Earlier I was fetching only one key/value pair from vault like below and it was giving me its value directly in the javascript code.

orgVault.get('key',function(err, securevalue) {

apigee.setVariable(req,"var_secretvalue",securevalue);

});

Now I am trying to fetch more number of key/value pairs and calling same get function number of times and setting those values to different variables like above.

I can see that the callback function returns the values properly and those are printed in node.js logs(and also observed that they will not be shown in the trace logs immediatedly like it happens for other variables created by different policies)

But when I am trying to use these variables in a service callout policy I am getting 'unresolved variable error'.

I also tried fetching these variables in a javascript(immediately after the node call execution) and setting these to different variables from the script but that is also not working.

I read on the page http://docs.apigee.com/api-services/content/access-flow-variables-nodejs the we need to set the variable in the request object only and by using the setVariable method like I used above.

Can somebody please help and let me know what am I missing here?

Thanks,

Santosh

Solved Solved
1 5 961
2 ACCEPTED SOLUTIONS

Hi,

I got this resolved by putting the statement resp.end('') in the last get call of the code.

Earlier I had put this at the end of createServer() method.

Thanks @rakshith for the pointer here.

Regards,

Santosh

View solution in original post

Hi,

I nested the subsequest org.getVault calls inside the callback of first and below code is working for me as a solution here.

var http = require('http');

var apigee = require('apigee-access');

var svr = http.createServer(function(req, resp) {

var orgVault = apigee.getVault('SampleVault', 'environment');

//Call to get the HostURL orgVault.get('HostURL',function(err, HostURL) {

if(err){ resp.end('Failed to get HostURL');

} else {

apigee.setVariable(req,"HostURL",HostURL);

console.log('HostURL',HostURL);

// Next call to get the TokenURL

orgVault.get('TokenURL',function(err, TokenURL) {

if(err){

resp.end('Failed to get TokenURL');

} else {

apigee.setVariable(req,"TokenURL",TokenURL);

console.log('TokenURL',TokenURL);

// Next call to get the CallURL

orgVault.get('CallURL',function(err, CallURL) {

if(err){

resp.end('Failed to get CallURL');

} else {

apigee.setVariable(req,"CallURL",CallURL);

console.log('CallURL',CallURL);

// Next call to get the Username

orgVault.get('Username',function(err, Username) {

if(err){

resp.end('Failed to get Username');

} else {

apigee.setVariable(req,"Username",Username);

console.log('Username',Username);

// Next call to get the Password

orgVault.get('Password',function(err, Password) {

if(err){

resp.end('Failed to get Password');

} else {

apigee.setVariable(req,"Password",Password);

console.log('Password',Password);

resp.end('All Calls were successful');

}

});

}

});

}

});

}

}):

}

});

});

svr.listen(9000, function() {

console.log('The server is listening on port 9000');

});

This is because there is no other logic in node.js than callback to check if the call got completed or not.

Using 'serial' execution model of express will be a overkill here as we are not doing anything but fetching values from vault.

Please let me know if this works fine or need any modifications/changes to make it better.

Thanks,

Santosh

View solution in original post

5 REPLIES 5

Hi,

I got this resolved by putting the statement resp.end('') in the last get call of the code.

Earlier I had put this at the end of createServer() method.

Thanks @rakshith for the pointer here.

Regards,

Santosh

Hi,

I see that sometimes the multiple org.getVault() methods are not fetching all the values.

I tried placing the resp.end() method in every callback method but it also does not help to retrieve calls consistently.

I can see the values getting fetched from vault via console.log statements but when I use them in the flow later it thows error like - 'Unresolved variable' <one random variable out of the list set in node, HostURL or CallURL>

Is there any approach where in I will be assured that all the values are returned by node?

Below is my code -

var http = require('http');

var apigee = require('apigee-access');

var svr = http.createServer(function(req, resp) {

var orgVault = apigee.getVault('SampleVault', 'environment');

orgVault.get('HostURL',function(err, HostURL) {

apigee.setVariable(req,"HostURL",HostURL);

console.log('HostURL',HostURL);

resp.end();

});

orgVault.get('TokenURL',function(err, TokenURL) {

apigee.setVariable(req,"TokenURL",TokenURL);

console.log('TokenURL',TokenURL);

resp.end();

});

orgVault.get('CallURL',function(err, CallURL) {

apigee.setVariable(req,"CallURL",CallURL);

console.log('CallURL',CallURL);

resp.end();

});

orgVault.get('Username',function(err, Username) {

apigee.setVariable(req,"Username",Username);

console.log('Username',Username);

resp.end();

});

orgVault.get('Password',function(err, Password) {

apigee.setVariable(req,"Password",Password);

console.log('Password',Password);

resp.end();

});

});

svr.listen(9000, function() {

console.log('The server is listening on port 9000');

});

Please let me know if there is anything missing here.

Thanks,

Santosh

Hi,

I just wanted to confirm if the vault also uses underlying cassandra database and will above issue may be related to something related to that.

Can anybody please help here.

Thanks,

Santosh

Hi,

I nested the subsequest org.getVault calls inside the callback of first and below code is working for me as a solution here.

var http = require('http');

var apigee = require('apigee-access');

var svr = http.createServer(function(req, resp) {

var orgVault = apigee.getVault('SampleVault', 'environment');

//Call to get the HostURL orgVault.get('HostURL',function(err, HostURL) {

if(err){ resp.end('Failed to get HostURL');

} else {

apigee.setVariable(req,"HostURL",HostURL);

console.log('HostURL',HostURL);

// Next call to get the TokenURL

orgVault.get('TokenURL',function(err, TokenURL) {

if(err){

resp.end('Failed to get TokenURL');

} else {

apigee.setVariable(req,"TokenURL",TokenURL);

console.log('TokenURL',TokenURL);

// Next call to get the CallURL

orgVault.get('CallURL',function(err, CallURL) {

if(err){

resp.end('Failed to get CallURL');

} else {

apigee.setVariable(req,"CallURL",CallURL);

console.log('CallURL',CallURL);

// Next call to get the Username

orgVault.get('Username',function(err, Username) {

if(err){

resp.end('Failed to get Username');

} else {

apigee.setVariable(req,"Username",Username);

console.log('Username',Username);

// Next call to get the Password

orgVault.get('Password',function(err, Password) {

if(err){

resp.end('Failed to get Password');

} else {

apigee.setVariable(req,"Password",Password);

console.log('Password',Password);

resp.end('All Calls were successful');

}

});

}

});

}

});

}

}):

}

});

});

svr.listen(9000, function() {

console.log('The server is listening on port 9000');

});

This is because there is no other logic in node.js than callback to check if the call got completed or not.

Using 'serial' execution model of express will be a overkill here as we are not doing anything but fetching values from vault.

Please let me know if this works fine or need any modifications/changes to make it better.

Thanks,

Santosh

Not applicable

Hi @santosh_ghalsasi,

Glad to know based on discussion you were able to move each of the vault operation into callback and ensure to only hit resp.end() when all are complete.

Thanks,

Rakshith