title: Write puppeteer json into html table in html and save it without jQuery
body: <p>Im collecting titles and images from website into a human-readable format.</p>
  
  <p>I use <code>fs.writeFile</code> and options are: </p>
  
  <ol>
  <li>save as html (where its opened locally) or,</li>
  <li>have it sent to email by nodemailer. </li>
  </ol>
  
  <p>Either way I need the info in table format in html. 
  Top row = Title, Price, Image (displayed, not link). 
  Columns =list of items. </p>
  
  <p>I added in a portion to convert JSON to html table but its messing up. Now the script does not run. Error is document not defined (in table formation). </p>
  
  <p>Separately, if theres any way to auto-send the list to emails daily without maintaining a server, kindly let me know too. </p>
  
  <pre class="lang-js prettyprint-override"><code>const puppeteer = require('puppeteer');
  const fs = require('fs');
  
  /* this gets the json data, all working ok */
  async function newCam() {
     const browser = await puppeteer.launch({ headless: false });
     let page = await browser.newPage();
     await page.goto('https://sg.carousell.com/search/products/?query=camera', { waitUntil: 'networkidle2' });
     let results = []; 
     let elements = await page.$$('div.U-U');
     for (let element of elements) {
        let listTitle  = await element.$eval('div.U-m', node =&gt; node.innerText.trim());
        let listImg    = await element.$eval('.U-p img', img =&gt; img.src);
        let listPrice  = await element.$eval('div.U-k :nth-child(1)', node =&gt; node.innerText.trim());
        results.push({ 
           'Title': listTitle,
           'Img':   listImg,
           'Px':    listPrice 
        });
     }
     await browser.close();
     return results;
  
  
     /* format json into table and feed into fs below */
        // get header keys
        var col = [];
        for (var i = 0; i &lt; results.length; i++) {
           for (var key in results[i]) {
                 if (col.indexOf(key) === -1) { col.push(key); }
           }
        }
  
        // create table 
        var table = document.createElement("table");
        var tr = table.insertRow(-1);                   // insert header row.
        for (var k = 0; k &lt; col.length; k++) {
           var th = document.createElement("th");      // fill header
           th.innerHTML = col[k];
           tr.appendChild(th);
        }
        // add json data as rows
        for (var a = 0; a &lt; results.length; a++) {
           tr = table.insertRow(-1);
           for (var f = 0; f &lt; col.length; f++) {
                 var tabCell = tr.insertCell(-1);
                 tabCell.innerHTML = results[a][col[f]];
           }
        }
  
     /* save to html on local drive with fs */ 
     fs.writeFile('/data.html', table, (err) =&gt; {
        if (err) throw err;
     });
  }
  newCam();
  </code></pre>