How to fix: Cannot create property 'header' on string

早过忘川 提交于 2019-12-24 02:43:14

问题


I'm trying to create a pdf using jspdf from json data. But it is causing an error which i'm not able to understand. Please help me solve the issue.

Data:

fileDataSpecific = [{ Date: "01/Jan/2019",
    ServerName: "prlhpcms01",
    ServerRequest: "Front End License returned",
    ServerStatus: "57 timed out",
    Time: "01:03:32",
    UserName: "root",
    srno: 1 }]

displayColumns = [
    { "val": "srno", "col": "Sr. No." },
    { "val": "ServerName", "col": "Server Name" },
    { "val": "UserName", "col": "User Name" },
    { "val": "Date", "col": "Date" },
    { "val": "Time", "col": "Time" },
    { "val": "ServerRequest", "col": "Server Request" },
    { "val": "ServerStatus", "col": "Server Status" }];

My function:

PrintData() {
    var pdfData = this.fileDataSpecific
    var pdfCol = this.displayedColumns
    var doc = new jsPDF()
    var col = [], row = []
    pdfCol.forEach(element => {
      col.push(element.col)
    });    
    pdfData.forEach(element => {
      var tempArray = []
      pdfCol.forEach(element1 => {
        tempArray.push(element[element1.val])
      });
      row.push(tempArray)
      tempArray = []
    });
    doc.autoTable(col, row);
    doc.save('Test.pdf');
  }

Error:

Please someone help! Thanks.


回答1:


You need treat your var pdfData = this.fileDataSpecific as array var pdfData = [this.fileDataSpecific]

fileDataSpecific = { Date: "01/Jan/2019",
    ServerName: "prlhpcms01",
    ServerRequest: "Front End License returned",
    ServerStatus: "57 timed out",
    Time: "01:03:32",
    UserName: "root",
    srno: 1 }

let displayedColumns = [
    { "val": "srno", "col": "Sr. No." },
    { "val": "ServerName", "col": "Server Name" },
    { "val": "UserName", "col": "User Name" },
    { "val": "Date", "col": "Date" },
    { "val": "Time", "col": "Time" },
    { "val": "ServerRequest", "col": "Server Request" },
    { "val": "ServerStatus", "col": "Server Status" }];


function PrintData() {
    var pdfData = [fileDataSpecific]
    var pdfCol = displayedColumns
    //var doc = new jsPDF()
    var col = [], row = []
    pdfCol.forEach(element => {
      col.push(element.col)
    });    
    pdfData.forEach(element => {
      var tempArray = []
      pdfCol.forEach(element1 => {
        tempArray.push(element[element1.val])
      });
      row.push(tempArray)
      tempArray = []
    });
    console.log(row)
    //doc.autoTable(col, row);
    //doc.save('Test.pdf');
  }
  
  PrintData();


来源:https://stackoverflow.com/questions/55965954/how-to-fix-cannot-create-property-header-on-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!