代码之家  ›  专栏  ›  技术社区  ›  SmartestVEGA

无法读取PDF组件中未定义的属性“forEach”

  •  0
  • SmartestVEGA  · 技术社区  · 6 年前

    ERROR TypeError: Cannot read property 'forEach' of undefined
        at Object.createModels (vendor.js:117599)
        at Object.jsPDF.API.autoTable (vendor.js:116203)
        at PDFComponent.push../src/app/Shared/Directives/Download/pdf-generator.component.ts.PDFComponent.downloadPDF (main.js:344)
        at Object.eval [as handleEvent] (ng:///AppModule/PDFComponent.ngfactory.js:13)
        at handleEvent (vendor.js:74287)
        at callWithDebugContext (vendor.js:75796)
        at Object.debugHandleEvent [as handleEvent] (vendor.js:75383)
        at dispatchEvent (vendor.js:70702)
        at vendor.js:71327
        at HTMLButtonElement.<anonymous> (vendor.js:95474)
    

    以下代码

    import { Component, Input, EventEmitter, Output,Inject  } from '@angular/core';
    import {BrowserModule} from '@angular/platform-browser';
    import {AuthService} from '../../Services/auth.service';
    import * as jsPDF from 'jspdf';
    import 'jspdf-autotable';
    
    declare var jsPDF: any; // Important
    @Component({
        selector: 'pdf-download',
        templateUrl: './pdf-generator.component.html',
    
    })
    export class PDFComponent   {
        @Input() downloadData: any; 
        //@Input() columns: any; 
        @Input() columns: any[] = []; 
        @Input() reportName: string; 
        constructor(private authService: AuthService) { }
        // public columns=[];
    
    
        downloadPDF(){
    
    
     for(var key in  this.downloadData[0])
             {
    
                this.columns.push({title:key,dataKey:key})
            }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   arun kumar    6 年前

    我认为这是一个问题,你如何对每个循环尝试使用下面的对象数组。可能对你有帮助

    例如:

    var downloadData=[{item:1,value:"one"},{item:2,value:"two"},{item:3,value:"three"}]
    
      var   columns=[]
         for(var key in  downloadData)
                 {
        
                    columns.push({title:downloadData[key].item,dataKey:downloadData[key].value})
                }
                
                console.log(columns)