SyntaxError: missing ) after argument list when exporting PDF in Rails App

SyntaxError: missing ) after argument list when exporting PDF in Rails App

我有一个语法错误,我很难弄清楚。下面的文件是 exportPDF.js,用于在 rails 的后台作业中将一些数据导出为 PDF。

在我的日志中,我在 运行 延迟作业时看到此错误。

launchChrome().then(async chrome => {
                    ^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
[Worker(host:MYMACBOOK-MacBook-Air.local pid:95907)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=8) (queue=default) FAILED (3 prior attempts) with Errno::ENOENT: No such file or directory @ rb_sysopen - /Users/alexanderkehaya/Desktop/redimatch/tmp/pdfs_addresses_d88439d0d90259b6bb55908f36163450.pdf

// exportPDF.js

const chromeLauncher = require('chrome-launcher');
const argv = require('minimist')(process.argv.slice(2))
const CDP = require('chrome-remote-interface');
const file = require('fs');

const viewportWidth = 1440;
const viewportHeight =  900;
const url = argv.url
const outputFileName = argv.o

function launchChrome(headless=true) {
  return chromeLauncher.launch({
    port: 9222, // Uncomment to force a specific port of your choice.
    chromeFlags: [
      '--window-size=412,732',
      '--disable-gpu',
      headless ? '--headless' : ''
    ]
  });
}

launchChrome().then(async chrome => {
  console.log(chrome.port)

  const client = CDP({ port: chrome.port }, async (client) => {
    console.log('CDP Running')

    const {DOM, Emulation, Network, Page, Runtime} = client;

    // Enable events on domains we are interested in.
    await Page.enable();
    await DOM.enable();
    await Network.enable();
    console.log('events enabled')

  // Set up viewport resolution, etc.
    const deviceMetrics = {
      width: viewportWidth,
      height: viewportHeight,
      deviceScaleFactor: 0,
      mobile: false,
      fitWindow: false,
    };

    await Emulation.setDeviceMetricsOverride(deviceMetrics);
    await Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});
    await Page.navigate({url});

    Page.loadEventFired(async () => {
      // If the `full` CLI option was passed, we need to measure the height of
      // the rendered page and use Emulation.setVisibleSize
      const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
      const {nodeId: bodyNodeId} = await DOM.querySelector({
        selector: 'body',
        nodeId: documentNodeId,
      });
      const {model: {height}} = await DOM.getBoxModel({nodeId: bodyNodeId});

      console.log('about to check window status')
      let v = await Runtime.evaluate({expression: 'window.status'})
      console.log('got first window status ' + v.result.value)

      while (!(v.result.value === 'READY FOR DOWNLOAD')) {
        console.log(v.result.value)
        v = await Runtime.evaluate({expression: 'window.status'})
      }


      const screenshot = await Page.printToPDF();
      const buffer = new Buffer(screenshot.data, 'base64');
      console.log(outputFileName)
      file.writeFile(outputFileName, buffer, 'base64', function(err) {
        if (err) {
          console.error(err);
        } else {
          console.log('PDF saved');
        }
        client.close();
        chrome.kill();
      });

    });


  })


}).catch((err) => console.log(err));

我错过了什么?我看到它告诉我确切的错误是什么,但我对 JavaScript 还是有点陌生​​,没有使用过异步,我不确定代码中的错误在哪里。

如果有帮助,这是一个 rails 应用程序 运行 rails 5 和 ruby 2.3.3

感谢观看!

尝试将 chrome 放在大括号内

launchChrome().then(async (chrome) => {

更新

嘿,您的代码似乎没有语法错误。您可以验证语法 here

问题可能是您正在使用的库有问题。