无法访问异步函数(JS)中的变量

时间:2019-04-23 20:48:25

标签: javascript

我的应用程序是在Node.js中制作的,我面临的问题是我无法从异步函数中的变量访问值。我有另一条路线(不是异步路线),一切正常,这是我收到此错误的地方:

  

协议错误(Emulation.setDeviceMetricsOverride):无效   参数width:期望的整数值;高度:整数值   预期的

app.get("/", cors(), (request, response) => {

  var filename = Date.now() + '.png';

  var width = request.query.width;
  var height = request.query.height;
  var url = request.query.url;

  console.log(width);
  console.log(height);
  console.log(url);

  (async () => {
    const browser = await puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox']
    });
    const page = await browser.newPage();
    await page.setViewport({ width: width, height: height });
    await page.goto(url, {
      waitUntil: 'networkidle2',
      timeout: 3000000
    });
    await page.screenshot({ path: './data/' + filename, fullPage: true });
    await browser.close();
  })()

})

我肯定是在发送整数。任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

顾名思义,

QueryString参数始终作为字符串求值。

您必须使用parseInt全局方法将这些字符串转换回整数。

var width = parseInt(request.query.width);
var height = parseInt(request.query.height);
相关问题