从module2.js调用nodejs module1.js然后解析一些变量

问题描述 投票:0回答:2

又是我。 :-D我的下一个怪异问题是:

我有2个nodejs模块:

//module1.js

const prompt = require('prompt');
var Excel = require('exceljs');


var wbCO = new Excel.Workbook();
var iCO = 1;
wbCO.xlsx.readFile('costumers.xlsx').then(function (){
  shCO = wbCO.getWorksheet("Sheet");
  while (iCO <= shCO.rowCount){  
    console.log(shCO.getRow(iCO).getCell(1).value +" - "+ shCO.getRow(iCO).getCell(2).value);
    iCO++;
  }

});

prompt.start();

prompt.get([{name:'costumer', required: true, conform: function (value) {
  return true;
}
}], function (key_err, key_result) {
    if (key_err) { return onErr(key_err); }

    var Ccostumer = shCO.getRow(key_result.costumer).getCell(2).value;
    var user = shCO.getRow(key_result.costumer).getCell(3).value;
    var pass = shCO.getRow(key_result.costumer).getCell(4).value;

    function onErr(key_err) {
      console.log(key_err);
      return 1;
    }  

});

       //module2.js

wb.xlsx.readFile('./'+Ccostumer+'/File.xlsx').then(function(){

sh = wb.getWorksheet("Sheet1");

    start_page(sh);
});

async function start_page(sh){  
  var i = 2;
  var result_id = 1;

    const browser = await puppeteer.launch({headless: true});

    while(i <= sh.rowCount){
    var result_cell = sh.getRow(i).getCell(3).text;
        await open_page(browser, result_cell, result_id);
        i++;
        result_id++;
  }
  browser.close();

}

        async function open_page(browser, result_cell, result_id) {

            const page = await browser.newPage();   
            page.setDefaultNavigationTimeout(100000);       

            await page.goto('https://www.mywebsite.com', {
                waitUntil: 'networkidle2'
            });
                //  authentication
                await page.waitFor('input[name="ctl00$ContentPlaceHolder1$Signin1$txtEmail"]');
                await page.$eval('input[name="ctl00$ContentPlaceHolder1$Signin1$txtEmail"]', elu => elu.value = user);
                await page.waitFor('input[name="ctl00$ContentPlaceHolder1$Signin1$txtPassword"]');
                await page.$eval('input[name="ctl00$ContentPlaceHolder1$Signin1$txtPassword"]', elp => elp.value = pass);
                await page.click('input[type="submit"]');
                await page.waitForNavigation();

                //search
                await page.waitFor('input[name="email"]');
                    await page.type('input[name="email"]', result_cell);
                await page.click('input[type="submit"]');

我正在尝试通过const md1 = require('./ module1.js');从module2.js调用module1.js;但是我没有得到变量,并且它们都同时运行。

所以这是我的问题:

1-在module1.js中做出选择后,如何运行module2.js,然后按Enter。

2-如何将这些变量从module1.js解析到module2.js(Ccostumer,用户,密码)。

javascript node.js variables node-modules prompt
2个回答
0
投票

在nodejs中,每个文件都有自己的作用域,并且每个文件中声明的变量和函数都属于它们。

为了使您能够从另一个.js文件访问函数或变量,您需要显式导出它们

file1.js

console.log('loading file 1') // runs when the file is loaded

function fileOneFunc () {
  // next line runs only when the function is called
  console.log('this is the fileOneFunc running')
}

module.exports = fileOneFunc

file2.js

// the following line will have access to the exported function from file1
const f1 = require('./file1')

console.log(typeof f1)

f1()

因此,如果只希望从module1.js内部进行调用时才运行module2.js中的代码,则必须将代码包装在module1.js中的函数中,然后如上所示将其导出。] >

然后在module2.js中执行您想说的话

const md1 = require('./module1.js')

然后,只要您认为合适,就调用md1函数。


0
投票

如果要从模块中返回某些内容,您只需要返回一些内容。如果您require另一个Node JS脚本,它不会自动返回任何内容,它主要是“运行”该脚本。

© www.soinside.com 2019 - 2024. All rights reserved.