Me again. :-D My next weird question is:
I have 2 nodejs modules:
//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"]');
I'm trying to call module1.js from module2.js by const md1 = require('./module1.js'); But I'm not getting the variables and both are running at same time.
So that's my questions:
1 - How to run module2.js just after I make my choice at module1.js then press ENTER.
2 - How to parse those variables from module1.js to module2.js (Ccostumer, user, pass).
If you want something in return from a module you simply need to return something. If you require
another Node JS Script, it does not automatically return anything, it mainly just "runs" the script.
Here is a basic example of 2 modules, one is called by the other and returns a value. In this example module1 is exporting a function, the function then is required and called in module2.
// module1.js
module.exports = () => {
/* doing stuff here */
return "someValue";
});
// module2.js
const value = require('./module1')();
console.log(value);
>> "someValue"
You have to reorder your logic a tiny bit and probably wrap somethings around functions. You want module2 to run after you made your choice in module1. Means, you want to call module2 from module1. Also means you need to require module2 in module1 and after you made your choice you run module2 with the output. In theory this would look like this :
Although I don't believe your code samples are complete, I'll try my best to give you a hint. But I won't do the work for you
// module1.js
const prompt = require('prompt');
const Excel = require('exceljs');
const module2 = require('./module2');
const workbook = new Excel.Workbook();
let counter = 1;
workbook.xlsx.readFile('costumers.xlsx').then( function () {
// Get Worksheet from Workbook
const sheet = workbook.getWorksheet("Sheet");
// Print Values of Worksheet
while (counter <= sheet.rowCount) {
const value1 = sheet.getRow(counter).getCell(1).value;
const value2 = sheet.getRow(counter).getCell(2).value;
console.log(`${value1} - ${value2}`);
counter++;
}
// Open prompt
prompt.start();
// Get result from prompt
prompt.get([
{
name:'costumer',
required: true
}
], function (key_err, key_result) {
if (key_err) { return console.log(key_err); }
const costumer = sheet.getRow(key_result.costumer).getCell(2).value;
const user = sheet.getRow(key_result.costumer).getCell(3).value;
const pass = sheet.getRow(key_result.costumer).getCell(4).value;
return module2(costumer, user, pass);
});
});
// module2.js
module.exports = function (costumer, user, pass) {
// Do something with costumer, user, pass
// the value you get in return from the prompt in module1.js
}
Make sure your module1.js and module2.js are in the same directory, otherwise require('./module2')
wouldn't work