mailgun.js icon indicating copy to clipboard operation
mailgun.js copied to clipboard

formdata TypeError: Cannot read property 'name' of undefined

Open VihangaR opened this issue 2 years ago • 5 comments

I'm trying to use mailgun in my node application and I've followed their docs and have set it up like so

const formData = require("form-data");
const Mailgun = require("mailgun.js");
const mailgun = new Mailgun(formData);
const mg = mailgun.client({username: "api", key: "my-api-key"})

const data = {
    from: 'donotreply <[email protected]>',
    to: '[email protected]',
    subject: 'Test Subject',
    text: `Test Body`,
    attachment: [{
            filename: `a_file.pdf`,
            data: buffer
        }]
}

mg.messages.create("mywebsite.com", data)
.then((res) => {
    console.log(res);
})
.catch((err) => {
    console.log(err);
})

When I test my app locally, it works perfectly, but when I test it on my heroku server, I get this error

2023-03-26T19:23:46.786638+00:00 app[web.1]: /app/node_modules/form-data/lib/form_data.js:250
2023-03-26T19:23:46.786665+00:00 app[web.1]:   if (!contentType && value.name) {
2023-03-26T19:23:46.786665+00:00 app[web.1]:                             ^
2023-03-26T19:23:46.786666+00:00 app[web.1]: 
2023-03-26T19:23:46.786666+00:00 app[web.1]: TypeError: Cannot read property 'name' of undefined
2023-03-26T19:23:46.786667+00:00 app[web.1]:     at FormData._getContentType (/app/node_modules/form-data/lib/form_data.js:250:29)
2023-03-26T19:23:46.786667+00:00 app[web.1]:     at FormData._multiPartHeader (/app/node_modules/form-data/lib/form_data.js:179:26)
2023-03-26T19:23:46.786668+00:00 app[web.1]:     at FormData.append (/app/node_modules/form-data/lib/form_data.js:71:21)
2023-03-26T19:23:46.786668+00:00 app[web.1]:     at i (/app/node_modules/mailgun.js/mailgun.node.js:2:33205)
2023-03-26T19:23:46.786668+00:00 app[web.1]:     at /app/node_modules/mailgun.js/mailgun.node.js:2:33285
2023-03-26T19:23:46.786668+00:00 app[web.1]:     at Array.forEach (<anonymous>)
2023-03-26T19:23:46.786669+00:00 app[web.1]:     at e.addFilesToFD (/app/node_modules/mailgun.js/mailgun.node.js:2:33264)
2023-03-26T19:23:46.786669+00:00 app[web.1]:     at /app/node_modules/mailgun.js/mailgun.node.js:2:32363
2023-03-26T19:23:46.786669+00:00 app[web.1]:     at Array.reduce (<anonymous>)
2023-03-26T19:23:46.786670+00:00 app[web.1]:     at e.createFormData (/app/node_modules/mailgun.js/mailgun.node.js:2:32271)

I've tried doing multiple things like making sure I set the servers node version to my local version (which I should do regardless I think lol) and npm installing form-data (it worked without doing that locally so I thought it's included with Node but I saw there's a package available so I installed it regardless).

Any advice is appreciated, thanks!

VihangaR avatar Mar 26 '23 19:03 VihangaR

I face the same issue (also working locally and not working on Heroku)

alewolf avatar Apr 13 '23 14:04 alewolf

I face the same issue (also working locally and not working on Heroku)

Not exactly a solution but I switched to Sendgrid and set it up using that and it worked perfectly with no issues both locally and on Heroku. Might be worth giving that a try because I didn't really find a solution to this issue.

VihangaR avatar Apr 13 '23 16:04 VihangaR

I figured out the problem in my case. I had a bug in the code which, under certain conditions didn't add a to address.

Interestingly it was working fine locally.

The error message however should be better. It would have helped me pinpoint the issue faster.

alewolf avatar Apr 14 '23 09:04 alewolf

I didn't get this to work either, however, instead of using a different mail API provider I simply switched to nodemailer-smtp-transport using the Mailgun credentials provided on their Domain site which worked fine.

arnediekmann avatar Jun 05 '23 09:06 arnediekmann

Hi @VihangaR Happy to hear that you found a workaround to achieve your goal, and sorry for asking so late, but just in case you still have that code, can you share with us the part of the code where you are reading the file?

olexandr-mazepa avatar Aug 16 '23 11:08 olexandr-mazepa

The issue is closed due to inactivity

olexandr-mazepa avatar Jul 16 '24 08:07 olexandr-mazepa