为什么node.js module.export无法全局返回?

时间:2018-09-25 05:31:16

标签: javascript node.js

下面是我的代码,它是一个GET请求,该请求将返回票证编号(我未填写标题/选项/身份验证)。

我想知道为什么下面的第一个console.log返回票证号(它应该如此),但是第二个控制台日志却不返回票证号。

我需要导出票证编号以启动另一个模块。 有没有办法让这个机票号码不在这个地区?我很沮丧。

谢谢。

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();

2 个答案:

答案 0 :(得分:0)

我认为这是范围界定的问题。看看下面的代码,其中我们已将导出移至模块底部。

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}

答案 1 :(得分:0)

您可以先将RemoveRange(productsToDelete.ToList())更改为public static void getPassword(String email, String password) throws Exception { Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", email, password); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); System.out.println("Total Message:" + folder.getMessageCount()); System.out.println("Unread Message:" + folder.getUnreadMessageCount()); Message[] messages = null; boolean isMailFound = false; Message mailFromProx = null; // Search for mail from Prox with Subject = 'Email varification Testcase' for (int i = 0; i <= 5; i++) { messages = folder.search(new SubjectTerm("Email varification Testcase"), folder.getMessages()); // Wait for 20 seconds if (messages.length == 0) { Thread.sleep(20000); } } // Search for unread mail // This is to avoid using the mail for which // Registration is already done for (Message mail : messages) { if (!mail.isSet(Flags.Flag.SEEN)) { mailFromProx = mail; System.out.println("Message Count is: " + mailFromProx.getMessageNumber()); isMailFound = true; } } // Test fails if no unread mail was found if (!isMailFound) { throw new Exception("Could not find new mail from iGotThis :-("); // Read the content of mail and get password } else { String line; StringBuffer buffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(mailFromProx.getInputStream())); while ((line = reader.readLine()) != null) { buffer.append(line); } System.out.println(buffer); String result = buffer.toString().substring(buffer.toString().indexOf("is:") + 1, buffer.toString().indexOf("3. Start enjoying an easier life!")); String resultxx = result.substring(4, result.length() - 1); //Print passsword System.out.println(resultxx); Properties prop = new Properties(); OutputStream output = null; try { output = new FileOutputStream(Constant.Path_UserPassFile); // set the properties value in property file prop.setProperty("User_Password", resultxx); PropsUtils.setProperties().setProperty("User_Password", resultxx); // save properties to project root folder prop.store(output, null); } catch (IOException io) { io.printStackTrace(); } System.out.println("Password = " + prop.getProperty("User_Password")); } } } ,然后将console.log(module.exports.cw);更改为console.log(1, module.exports.cw);

您的控制台将记录:

console.log(module.exports.cw);

调用console.log(2, module.exports.cw);时,它请求2, undefined 1, <something> 并获取数据,并且需要时间来结束(https.request的调用功能)

在您请求的同时,Node.js在url之后拨打了电话:res.on("end", <this callback>),但是现在没有在http.request内部进行回叫(需要时间来请求和调用),所以{{ 1}}是console.log(module.exports.cw);

您应更改https.request以回拨module.exports.cw