Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read properties of undefined (reading 'type') #94

Closed
zees98 opened this issue Sep 20, 2021 · 7 comments
Closed

TypeError: Cannot read properties of undefined (reading 'type') #94

zees98 opened this issue Sep 20, 2021 · 7 comments

Comments

@zees98
Copy link

zees98 commented Sep 20, 2021

Hello,
I am trying to use html2docx for a react project. But for some reason it keeps crashing with the error:
TypeError: Cannot read properties of undefined (reading 'type')

I have tried it with the example and then changed it. Both codes yield the same error.

var html = `<html lang="en">
                      <head>
                        <meta charset="UTF-8" />
                        <title>Document</title>
                      </head>
                    <body>` +
                    data +
                    `
                    </body>
                  </html>`;
 HTMLtoDOCX(data, null, {
                    table: { row: { cantSplit: true } },
                    footer: true,
                    pageNumber: true,
                  })
                    .then((fileBuffer) => {
                      fs.writeFile("./document.docx", fileBuffer, (error) => {
                        if (error) {
                          console.log("Docx file creation failed");
                          return;
                        }
                        console.log("Docx file created successfully");
                      });
                    })
                    .catch((err) => {
                      alert(err);
               });

This is my rendition of the code.

My enviroment:

macOS: 12.0 Beta
package version: 1.2.4
node version: v16.3.0

Thank you

@privateOmega
Copy link
Owner

Could you possibly post the html string you used? looks like either a bug or an edge case

@zees98
Copy link
Author

zees98 commented Sep 22, 2021

Apologies for replying so late.

This is my html string.
<h2>Hello, World!</h2><p>This is some text over here.</p>

Edit:
I also have tested this on Windows and got the same error.

@privateOmega
Copy link
Owner

@zees98 No worries, Its quite unlikely that the package failed with that html string because I am able to generate it without any issue on my laptop with react-example in the examples/ in this repo.

Could you please let me know what's the end result after clone this repository, and run the example and try it out? I am super curious about this issue.

html-to-docx.docx

@zees98
Copy link
Author

zees98 commented Sep 22, 2021

Thank you for the reply.
I will clone the example and let you know.
Cheers

@zees98
Copy link
Author

zees98 commented Sep 22, 2021

@privateOmega
Hello again.
So I tried the example and the error I was facing is gone. I also took some assistance from one of your closed issues since my use case is with ReactJS as well.
But now I am receiving another error.
Unhandled Rejection (TypeError): Cannot call a class as a function

My HTML string:

<!DOCTYPE html><html lang="en">
                      <head>
                        <meta charset="UTF-8" />
                        <title>Document</title>
                      </head>
                    <body><h2>Hello World!</h2>
                    </body>
                  </html>

The altered method:

import HTMLtoDOCX from 'html-to-docx';
import { save } from 'file-save'

const html2DocConverter = async () => {
    var html = `<!DOCTYPE html><html lang="en">
                      <head>
                        <meta charset="UTF-8" />
                        <title>Document</title>
                      </head>
                    <body>` +
      text +
      `
                    </body>
                  </html>`;
    console.log(html);
    const fileBuffer = await HTMLtoDOCX(html, null, {
      table: { row: { cantSplit: true } },
      footer: true,
      pageNumber: true,
    });
    await save(fileBuffer, 'trial.docx');
  }

@privateOmega
Copy link
Owner

Oh just noticed that the same issue was reported in #92 , hence closing the issue to track it there.

@privateOmega
Copy link
Owner

@zees98 I might possibly have figured out the issue, please follow the step in the other issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants