Hi,
I am not able to extract service callout response variables in node.js:
Used following in my node js:
var svr = http.createServer(function(req, resp) {
console.log("insidefunction");
var testVariable = apigee.getVariable(req,'sample');
console.log("testVariable ", testVariable);
var abc = JSON.stringify(testVariable);
console.log("info abc235", abc);
var testVariable1 = {};
testVariable1=JSON.parse(abc);
console.log("info testVariable1", testVariable1); //console.log('sampleVar2212342',JSON.stringify(testVariable));*/ //var testVariable1 = JSON.parse(testVariable);
console.log('cust212 ',testVariable1.customerInfo.customer);
console.log('customerI2 ','Hello, Node!');
resp.end('Hello, Node!'); });
console.log('cus ','Hello,');
svr.listen(process.env.PORT || 8000, function() { console.log('Node HTTP server is listening'); });
not able to extract testVariable1.customerInfo and further , though it is been parsed as json object and in console log i am getting it printed , but on the next line if we extract the variable it is giving undefined.
var http = require('http'); var apigee = require('apigee-access'); var d = new Date(); console.log("date "+ d); console.log(d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds()); console.log('node.js application starting2542543...'); var svr = http.createServer(function(req, resp) { var testVariable = apigee.getVariable(req,'sample'); var abc = JSON.parse(JSON.stringify(testVariable)); console.log("info abc", abc); console.log("abc customerInfo", abc.customerInfo); console.log("abc customer3242", abc.customerInfo.customer); resp.end('Hello, Node!'); }); svr.listen(process.env.PORT || 8000, function() { console.log('Node HTTP server is li'); });
This is the sample variable response : {"customerInfo":{"customer":{"@customerId":"124545"}}}
Thanks @Ashish sharma,
var testVariable = apigee.getVariable(req,'sample');<br>
the value returned is already a string, the following operation can be skipped.
JSON.stringify(testVariable)<br>
Try parsing the value directly -
var abc = JSON.parse(testVariable);<br>
Let me know if that works!
getting following error:
*** Starting script date Thu Dec 29 2016 05:04:05 GMT-0000 (UTC) 5-4-5 node.js application starting2542543... Node HTTP server is li FAILED SETTING MESSAGES {"message":"Access to Java class \"com.apigee.protocol.http.msg.HTTPResponse\" is prohibited. (errorConfig_js#1232)","fileName":"errorConfig_js","lineNumber":1232} Error content exists Javascript error finished determining error type, handling.. Possible error sources: ServiceCallout, non-success, javascript [DEBUG]: errResp = {"fault":{"faultstring":"JavaScript execution failed: Unterminated string literal: SyntaxError: Unterminated string literal\n at /organization/environment/api/coreNode.js:12\n at emit (events.js:98)\n at adaptorhttp.js:540\n at domain.js:183\n at domain.js:123\n at adaptorhttp.js:539\n at adaptorhttp.js:649\n","detail":{"errorcode":"scripts.node.ScriptExecutionError"}}} Filters
This error i am getting when i am parsing directly : var abc = JSON.parse(testVariable);
Checked the syntax , having : var abc = JSON.parse(testVariable); on line 12 .which is giving the error
Let me cross check with the same use-case.
Check for syntax -
Unterminated string literal
is the error.
The following snippet works fine for me -
var express = require('express'); var app = express(); var apigee = require('apigee-access'); app.get('/apigee',function(req,res){ var testVariable = apigee.getVariable(req,'sample'); var abc = JSON.parse(JSON.stringify(testVariable)); console.log(abc) console.log(abc.customerInfo) res.send("Success"); }) var server = app.listen(8081);
In my case, I have assigned the variable sample using Javascript Policy.
var express = require('express'); var app = express(); var apigee = require('apigee-access'); console.log("Node server started!!!!"); app.get('/customers/12345678/info',function(req,res){ var testVariable = apigee.getVariable(req,'sample'); var abc = JSON.parse(JSON.stringify(testVariable)); console.log(abc); console.log(abc.customerInfo); res.send("Success"); }) console.log("Node server ended!!!"); var server = app.listen(8081); Logs: *** Starting script Node server started!!!! Node server ended!!! Filters
Though it is giving 200 but it is not printing anything in console, Also '/customers/12345678/info' is again dynamic in my case , hence need to again extract the variable in node js.
@Nisha Mallesh: Still not able to get succesfull response.
The above is the screenshot for the node js logs.
Also, make use of path params for dynamically populating the customer values -
/customers/12345678/info'