question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

formData property 'name' undefined when sending an email

See original GitHub issue

Hello! I’ve just landed on a project that was using mailgun-js and I’m having some trouble migrating to mailgun.js package. The issue seems to be with form-data package. The client initialization is correct and when running the function return await mailgun.messages.create(mailgunDomain, messageData) the node app crashes with the following error

Request error! TypeError : Cannot read property 'name' of undefined 
 TypeError: Cannot read property 'name' of undefined
    at FormData._getContentDisposition (/Users/PERE/Developer/marketplace-mary-services/node_modules/form-data/lib/form_data.js:227:40)
[...]

I see that form-data expects a value that I think mailgun.js should provide, maybe?

Here is my initialization

const Mailgun = require('mailgun.js')
const formData = require('form-data')
const mailgun = new Mailgun(formData)
const { MAILGUN_KEY, MAILGUN_HOST } = process.env
const mg = mailgun.client({
  username: 'api',
  key: MAILGUN_KEY,
  url: MAILGUN_HOST
})

And the packages installed are the following in package.json

"form-data": "^4.0.0",
"mailgun.js": "^5.2.2",

What am I missing here?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
olexandr-mazepacommented, May 9, 2022

It happens because the SDK handles property with the name ‘file’ as a file description. Here is a block of code that does that. https://github.com/mailgun/mailgun.js/blob/9d56b32d3d5f4fc7903dcdb368e338dc7b1a694e/lib/formDataBuilder.ts#L17-L21

I will change this behavior and publish the fix soon. Until I do that, as a temporary solution you can simply remove this property from the message data.

If I am not mistaking template is already compiled to HTML after these lines https://github.com/pmontp19/mailgun-example/blob/57e1c6c30a1960e44c917ad070917103eba34b59/index.js#L17-L18 so it looks like you don’t need the filename anymore in message data.

0reactions
ralyodiocommented, Nov 1, 2022

I’m still having this problem…and i’m not passing a file property.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'name' of undefined #336
What I noticed is that the issue comes up if any of the properties that you are setting on the form data has...
Read more >
TypeError: Cannot read property 'name' of null NodeJs
This issue arises because in the formData key of requestPromise method you do not need to actually pass formData but an object ...
Read more >
Cannot read property 'name' of undefined while using form ...
I am using `node-fetch` and in my call I am using `form-data` my code looks something ... TypeError: Cannot read property 'name' of...
Read more >
Forms in HTML documents - W3C
A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element...
Read more >
I keep on having an issue of "TypeError: Cannot read property ...
I keep on having an issue of "TypeError: Cannot read property 'Email' of undefined" when trying to submit HTML form data to my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found