0

So I generate some HTML and I have an object that contains the HTML called html and I want to write this to a new file but right now it isn't working because and just has NaN in my saved file. This is what I have so far

import mjml2html from 'mjml'
import Handlebars from 'handlebars'
import fs from 'fs'

const template = Handlebars.compile(`
  <mjml>
  <mj-body>
    <mj-section background-color="#F0F0F0" padding-bottom="0">
      
      <mj-column  padding-left="70px" width="250px">
        
        <mj-text font-style="italic" font-size="22px" color="#626262">watFriends</mj-text>
        
      </mj-column>
      
      <mj-column width="170px"> 
            <mj-image width="30px" src={{logo}} />
        </mj-column>
    </mj-section>
    
    <mj-section background-color="#FAFAFA">
      <mj-column width="400px">
        <mj-text font-style="italic" font-size="15px" font-family="Helvetica Neue" color="#626262">
          Dear {{firstName}},
        </mj-text>
        <mj-text color="#525252">{{message}}
        </mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
    
`)
const context = {
  firstName: '',
  message: 'hello',
  logo: 'logo.png',
}
const mjml = template(context)
const html = mjml2html(mjml)
console.log(html)

fs.writeFile('new.html', html.toString(), { encoding: 'utf8' }, function (err) {
  if (err) {
    return console.log(err)
  }
  console.log('The file was saved!')
})

1 Answer 1

2

mjml2html returns an object like {html: ' ...html here..', json: {}, errors: []} and toString() of this object is string "[Object object]" which you are writing to a file. Change

const html = mjml2html(mjml)

to

const {html} = mjml2html(mjml)

and all will be fine

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this was exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.