0

我正在尝试从节点中的服务器文件中重构 docxtemplater 代码。req.body json 对象在那里工作正常,甚至可以在 doc.setData() 方法中进行console.logged。但是,当我尝试运行服务器时,出现此错误:

firstName: data.firstName,
               ^      
TypeError: Cannot read property 'firstName' of undefined     

这是我的 docxtemplater.js 文件。

const express = require('express');
const Docxtemplater = require('docxtemplater');
const JSZip = require('jszip');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');

function estateDoc (data) {

let content = fs.readFileSync(path.resolve(__dirname, 'docxGen.docx'), 'binary');

let zip = new JSZip(content);

let doc = new Docxtemplater();
doc.loadZip(zip);

doc.setData({
  firstName: data.firstName,
  lastName: data.lastName,
  middleName: data.middleName,
  suffix: data.suffix,
  socialSecurity: data.socialSecurity,
  address: data.address,
  telephone: data.telephone,
  heir: data.heir
});
  try {
      doc.render()
  }
  catch (error) {
      var e = {
          message: error.message,
          name: error.name,
          stack: error.stack,
          properties: error.properties,
      }
      console.log(JSON.stringify({error: e}));
      throw error;
  }
  var buf = doc.getZip()
               .generate({type: 'nodebuffer'});
  fs.writeFileSync(path.resolve(__dirname + '/doc-sender-catcher', 'output.docx'), buf)
};
estateDoc();

module.exports = {estateDoc};
4

1 回答 1

0

所以我把这个留给任何新的节点重构的人。不要声明你的函数,然后在导出和运行它之前调用它。我的问题是我在数据参数为空的情况下运行函数的最后一行......

于 2017-02-13T21:41:29.247 回答