Javascript 无法读取属性';forEach&x27;PDF组件中未定义的

Javascript 无法读取属性';forEach&x27;PDF组件中未定义的,javascript,arrays,angular,typescript,Javascript,Arrays,Angular,Typescript,我犯了以下错误,请看下面:我做错了什么 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-gene

我犯了以下错误,请看下面:我做错了什么

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)

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

例如:

var downloadData=[{item:1,值:“一”},{item:2,值:“二”},{item:3,值:“三”}]
变量列=[]
for(下载数据中的var键)
{
columns.push({title:downloadData[key].item,dataKey:downloadData[key].value})
}

console.log(columns)
该错误似乎是在jsPDF
autoTable
构造函数中抛出的。你在哪里呼叫/初始化它?对我来说,这个错误可能与这个构造函数有关:我认为,您需要检查
this.downloadData
是否可用,然后只允许
for
循环。
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})
        }
}