title: Using Node.js to generate dynamic word document using Database value
body: <p>I am trying to dynamically populate the WORD Document using <code>npm docx</code>. I am trying to read the data from the SQLite database but due to async node js property the values are not getting into the variable and it shows <code>undefined</code>. If I make the function synchronous the <code>npm docx</code> throws error and doesn't populate the document.</p>
  
  <p>package.json</p>
  
  <pre><code>{
    "name": "demoName",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
      "body-parser": "^1.19.0",
      "docx": "^5.1.1",
      "express": "^4.17.1",
      "md5": "^2.2.1",
      "sqlite3": "^4.2.0"
    }
  }
  </code></pre>
  
  <p>index.js</p>
  
  <pre><code>const docx = require('docx');
  var express = require('express');
  var app = express();
  var db = require("./database.js")
  
  var bodyParser = require("body-parser");
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  
  const { AlignmentType, Document, Footer, Header, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType, Table, TableCell, TableRow } = docx;
  
  app.get("/doc", async(req, res) =&gt; {
      var sql = "select * from DocDetails"
      var params = []
      //let DocDetailsData;
      //let DocDetailsData = [{docId: "Some Doc Id"}];
      const DocDetailsData = db.all(sql, params, (err, rows) =&gt; {
  
          if (err) {
            res.status(400).json({"error":err.message});
            return;
          }
          console.log(rows[0]);
          return rows[0];
  
      });
      console.log(DocDetailsData.docId);
  
      const doc = new Document();
      doc.addSection({
          children: [
              new Paragraph({
                  children: [
                      new TextRun({
                          text: "DEMO TEST DOCUMENT"
                      }),
                      new TextRun({
                          text: DocDetailsData.docId,
                      }),
                  ]
              }),
          ],
      });
  
      const b64string = await Packer.toBase64String(doc);
      res.setHeader('Content-Disposition', 'attachment; filename=My Document.docx');
      res.send(Buffer.from(b64string, 'base64'));
  });
  
  madeDoc = function(){
  
  
  }
  
  app.use(function(req, res){
      res.status(404);
  });
  
  var server = app.listen(4041, function () {
     var host = 'localhost'
     var port = server.address().port
  
     console.log("Example app listening at http://%s:%s", host, port)
  })
  </code></pre>
  
  <p>database.js</p>
  
  <pre><code>var sqlite3 = require('sqlite3').verbose()
  var md5 = require('md5')
  
  const DBSOURCE = "db.sqlite"
  
  let db = new sqlite3.Database(DBSOURCE, (err) =&gt; {
      if (err) {
        // Cannot open database
        console.error(err.message)
        throw err
      }else{
          console.log('Connected to the SQLite database.')
          db.run(`CREATE TABLE DocDetails (
              id INTEGER PRIMARY KEY,
              docId text NOT NULL,
              version float NULL,
              editedBy text NULL,
              editedDate text NULL,
              effectiveDate text NULL)`,
            (err) =&gt; {
                if (err) {
                    // Table already created
                    console.log('Table not created');
                }else{
                    console.log('Table created');
                    var insert = 'INSERT INTO DocDetails (docId, version, editedBy, editedDate, effectiveDate) VALUES (?,?,?,?,?)'
                    db.run(insert, ["NESS-RD-TEMP-EDCHB",2.1, "manab", "18-Jul-2017", "18-Jul-2020"])
                }
            })
      }
  });
  
  
  module.exports = db
  </code></pre>
  
  <p>If you go to localhost:4041/doc, a word document should get downloaded but it shows only one row and not the data from database. I need the database value to be populated in the doc.
  Thanks.</p>