简体   繁体   English

将变量传递给主 function Nodejs

[英]Pass variable to main function Nodejs

Basically I'm trying to pass the "Browser" variable on to my main function, to pass on to "CheckStock".基本上我试图将“浏览器”变量传递给我的主要 function,以传递给“CheckStock”。 I've tried many thing but just can't seem to find a way how to do this.我已经尝试了很多事情,但似乎无法找到一种方法来做到这一点。 If anyone knows how to, please let me know.如果有人知道怎么做,请告诉我。 Thanks in advance: :)提前致谢: :)

// STARTUP BROWSER & GO TO URL
async function openBrowser(monitorURL) {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto(monitorURL)
  return browser, page;
}

/* 
...
...
...
other code
...
...
...
*/

// MAIN FUNCTION
async function monitor(monitorURL){
  const page  = await openBrowser(monitorURL);
  await checkStock(page, browser)
}

You need to change how you return the two values to this (so they are both available in a returned object):您需要更改将这两个值返回到 this 的方式(因此它们都可以在返回的对象中使用):

async function openBrowser(monitorURL) {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto(monitorURL)
  return {browser, page};
}

And, then how you get them both out of that object:然后,如何让它们都脱离 object:

// MAIN FUNCTION
async function monitor(monitorURL){
  const {page, browser} = await openBrowser(monitorURL);
  await checkStock(page, browser);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM