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: htmlToText is not a function #218

Closed
shunyun-bp opened this issue Dec 29, 2020 · 9 comments
Closed

TypeError: htmlToText is not a function #218

shunyun-bp opened this issue Dec 29, 2020 · 9 comments

Comments

@shunyun-bp
Copy link

Minimal HTML example
using any html

Options
using default options

Observed output

TypeError: htmlToText is not a function
          at /Users/asfd/code/integration_template/src/bpIntegrationFuncs.js:168:62
          at /Users/asdf/code/integration_template/src/node_modules/ramda/src/internal/_curryN.js:46:27
          at /Users/asdf/code/integration_template/src/node_modules/ramda/src/internal/_arity.js:11:19
          at _ifElse (/Users/asdf/code/integration_template/src/node_modules/ramda/src/ifElse.js:39:54)
          at f1 (/Users/asdf/code/integration_template/src/node_modules/ramda/src/internal/_curry1.js:19:17)
          at Object.<anonymous>.exports.handler (/Users/asdf/code/integration_template/src/index.js:22:21)

Expected output
Change log says that fromString is deprecated, so trying to use htmlToText. Not sure why htmlToText is not recognized as a function in my code. Tried wrapping it in another function, in an IFFE, nothing works except using the fromString function.

Version information

  • html-to-text: 6.0.0
  • node: 12.16.2

I'm trying to use htmlToText inside of a curried function (using ramda curryN) and get "TypeError: htmlToText is not a function". It DOES work if instead I use "fromString".
example of failing code:
var objectFromEMail = R.curryN(3, (splitCharsExp, matchCharsExp, payload) => { let rawText = payload.text ? payload.text : payload.html ? htmlToText(payload.html) : Error - failed to parse email body (no text or html)! ...
example of successful code:
var objectFromEMail = R.curryN(3, (splitCharsExp, matchCharsExp, payload) => { let rawText = payload.text ? payload.text : payload.html ? fromString(payload.html) : Error - failed to parse email body (no text or html)! ...

@KillyMXI
Copy link
Member

Pay attention to how the import is done.
There is no default export.

const { htmlToText } = require('html-to-text');

I should've made it more clear in the changelog.

@shunyun-bp
Copy link
Author

Found the problem; I hadn't imported with a specific version so I was using 5.1.1. Already had it in my node_modules and it's required by mailparser 2.7.7. Upgrading html-to-text to 6.0.0 solves the problem.

@lediokoci1
Copy link

image

@ritik2727
Copy link

ritik2727 commented Nov 16, 2023

Excerpt from README:

  • fromString method - use convert or htmlToText instead;
  • positional arguments in BlockTextBuilder methods - pass option objects instead.

Selectors

Some example:

const { convert } = require('html-to-text');

const html = '<a href="/page.html">Page</a><a href="!#" class="button">Action</a>';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]

@mithunmunsi
Copy link

mithunmunsi commented Nov 25, 2023

in my case: it shows like this... how can I solve this problem.. please help !

{
    "status": "error",
    "error": {
        "statusCode": 500,
        "status": "error"
    },
    "message": "convert is not defined",
    "stack": "ReferenceError: convert is not defined\n    at Email.send (/Users/mithun/Library/CloudStorage/nextis/utils/email.js:45:25)\n    at Email.sendWelcome (//Users/mithun/Library/CloudStorage/nextis/utils/email.js:58:16)\n    at /Users/mithun/Library/CloudStorage/nextis/controllers/authController.js:49:33\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
}

@CodeByAnkita
Copy link

CodeByAnkita commented Dec 5, 2023

In my app look like this when i add user to with api then i get this error

const htmlToText = require('html-to-text');
const mailOptions = {
      from: this.from,
      to: this.to,
      subject,
      html,
      text: htmlToText.fromString(html)
    };
**ERROR**
{TypeError: htmlToText.fromString is not a function
    at Email.send (C:\\Code\\Nodejs\\Natours App\\utils\\email.js:50:24)
    at Email.sendWelcome (C:\\Code\\Nodejs\\Natours App\\utils\\email.js:58:16)
    at C:\\Code\\Nodejs\\Natours App\\controllers\\authController.js:43:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

@mithunmunsi
Copy link

In my app look like this when i add user to with api then i get this error

const htmlToText = require('html-to-text');
const mailOptions = {
      from: this.from,
      to: this.to,
      subject,
      html,
      text: htmlToText.fromString(html)
    };
**ERROR**
{TypeError: htmlToText.fromString is not a function
    at Email.send (C:\\Code\\Nodejs\\Natours App\\utils\\email.js:50:24)
    at Email.sendWelcome (C:\\Code\\Nodejs\\Natours App\\utils\\email.js:58:16)
    at C:\\Code\\Nodejs\\Natours App\\controllers\\authController.js:43:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I have the same problem...

@KillyMXI
Copy link
Member

KillyMXI commented Dec 5, 2023

@mithunmunsi if your message above from last week is accurate - then it is not the same problem.

If you're using fromString with version 9 or newer - you cannot read documentation.

If you have an error convert is not defined - it is a different problem. Still likely a user error.
It seems most often occurring to users not able to do through research before writing here. Your message is not exception - it tells me nothing and just shows you're not able to understand what you're doing.
And those who do a thorough research - are likely to resolve it by themselves.
So, I'm inclined to believe it is still a user error.

The only case of reporting the issue then finding the cause is available above.
It involves a misalignment between what version you think you're using and what version is actually used.

@didierbonga90
Copy link

didierbonga90 commented Dec 11, 2023

const htmlToText = require('html-to-text');
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: htmlToText.fromString(html)
};

@mithunmunsi Try this:

const { convert }= require('html-to-text');
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: convert(html)
};

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

7 participants