0

Below is my code, it is a GET request that will return a Ticket Number (I left the headers/options/auth out).

I want to know why the first console.log below returns a ticket number (as it should), but the second console log does not return a ticket number.

I need to export the ticket number to fire another module. Is there a way to get that ticket number outside of this area? I am stumped.

Thanks in advance.

options.path += orderNumber;
var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    global.body = Buffer.concat(chunks);
    module.exports.cw = JSON.parse(body.toString()).customerOrderNo.replace(/\D/g,'');
    **console.log(module.exports.cw);**
  });  
});
**console.log(module.exports.cw);**
req.write(JSON.stringify({ id: 0,
  description: 'maxLength = 100',
  url: 'Sample string',
  objectId: 0,
  type: 'Sample string',
  level: 'Sample string',
  memberId: 0,
  inactiveFlag: 'false' }));
req.end();
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Patrick Roberts Sep 25 '18 at 05:33
  • "Is there a way to get that ticket number outside of this area?" - No. "I need to export the ticket number to fire another module." - What do you mean, _fire another module_? – Amadan Sep 25 '18 at 05:46

2 Answers2

0

I think it is problem of scoping. Take a look at below code where we have moved export to bottom of module.

options.path += orderNumber;
var ticketNumber;
var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    global.body = Buffer.concat(chunks);
    ticketNumber = JSON.parse(body.toString()).customerOrderNo.replace(/\D/g,'');
    **console.log(ticketNumber);**
  });  
});
**console.log(ticketNumber);**
req.write(JSON.stringify({ id: 0,
  description: 'maxLength = 100',
  url: 'Sample string',
  objectId: 0,
  type: 'Sample string',
  level: 'Sample string',
  memberId: 0,
  inactiveFlag: 'false' }));
req.end();

module.exports.getCw = function(){return ticketNumber}
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19
  • `module.exports.cw = ticketNumber;` will set `module.exports.cw` = `undefined`, you can change it to `module.exports.getCw = function(){return ticketNumber}` to get it. – hong4rc Sep 25 '18 at 07:55
0

You can change first console.log(module.exports.cw); to console.log(1, module.exports.cw); and console.log(module.exports.cw); to console.log(2, module.exports.cw);

Your console will log :

2, undefined
1, <something>

When you call https.request, it request to url and get data, and need time to end it (call function of res.on("end", <this callback>))

While you request, Node.js call line after http.request : console.log(module.exports.cw);, but now call back inside https.request wasn't call (need time to request and call) so module.exports.cw is undefined

You should change req.write to call back of your https.request

hong4rc
  • 3,999
  • 4
  • 21
  • 40