javascript - 无法读取 PDF 组件中未定义的属性 'forEach'

标签 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-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})
        }
}

最佳答案

我认为你在每个循环中如何尝试使用以下对象数组是一个问题。它可能对你有帮助

例如:

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)

关于javascript - 无法读取 PDF 组件中未定义的属性 'forEach',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56020554/

相关文章:

javascript - 如何使用 Jest 和 Enzyme 测试随时间更新的 React 组件?

javascript - 将事件传递给回调函数

从文件中提取后无法将提取的数字转换为 int

python - 给定置换数组,找到置换

javascript - 使用 Handlebars 中的 if 更改单击功能

c# - 为什么不能在数组内交换匿名类型的属性?

angular - 如何将点击事件绑定(bind)到整个组件(又名 :host)?

javascript - Angular 5 : JavaScript CustomEvent (Polyfill)

angular - 直接访问 FormControl 元素而不是使用 form.get

javascript - 本地系统日期格式