最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Use pdfMake with data input in Angular 2TypeScript - Stack Overflow

matteradmin2PV0评论

I'm trying to create PDF from data input in an Angular 2 project. To do this, I integrated pdfMake. Creating static PDF works just fine, but the moment I try to print out some values, it goes wrong.

Please have a look at this plunker:

I manage to put an object into my buildPdf function and it gets loged to the console. But when I try to add one of my object values to the pdfMake docdefinition, I'm getting the error message:

ORIGINAL EXCEPTION: TypeError: Cannot read property 'firstName' of undefined

var docDefinition = {
    content: [{
        text: 'My name is: ' + pdfContent.firstName  + ' ' + pdfContent.lastName + '.'
    }]
}

I tried to rule out a typing issue by rewriting my buildPdf.js in typescript, which looked something like this:

var buildPdf: (value: {firstName: string, lastName: string}) => any =
function(value: {firstName: string, lastName: string}): any {
    let docDefinition:any = {
        content: [{
            text: 'This is a test.'
        }]
    }
    console.log(value);
    return docDefinition;
}

The result stayed the same, though.

Why am I able to console.log the object but not to access its properties? What have I missed?

I'm trying to create PDF from data input in an Angular 2 project. To do this, I integrated pdfMake. Creating static PDF works just fine, but the moment I try to print out some values, it goes wrong.

Please have a look at this plunker: https://plnkr.co/edit/7tPFTAjKWwaRp1IbT0Tg?p=preview

I manage to put an object into my buildPdf function and it gets loged to the console. But when I try to add one of my object values to the pdfMake docdefinition, I'm getting the error message:

ORIGINAL EXCEPTION: TypeError: Cannot read property 'firstName' of undefined

var docDefinition = {
    content: [{
        text: 'My name is: ' + pdfContent.firstName  + ' ' + pdfContent.lastName + '.'
    }]
}

I tried to rule out a typing issue by rewriting my buildPdf.js in typescript, which looked something like this:

var buildPdf: (value: {firstName: string, lastName: string}) => any =
function(value: {firstName: string, lastName: string}): any {
    let docDefinition:any = {
        content: [{
            text: 'This is a test.'
        }]
    }
    console.log(value);
    return docDefinition;
}

The result stayed the same, though.

Why am I able to console.log the object but not to access its properties? What have I missed?

Share Improve this question edited Mar 1, 2016 at 10:23 Günter Zöchbauer 659k234 gold badges2.1k silver badges1.6k bronze badges asked Mar 1, 2016 at 10:19 mgogamgoga 331 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Seems to be caused by this line

buildPdf: any = new buildPdf();

I don't see what you need it for. It throws because it accesses the passed value but here none is passed.

See https://plnkr.co/edit/NPLHxVgC5LthA4g2WpUx?p=preview for the working Plunker.

Post a comment

comment list (0)

  1. No comments so far