cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.42k stars 595 forks source link

🐛 BUG: mailgun.js able send email in localhost, but failed in Cloudflare page and it returns Cannot convert object to primitive value #4646

Open joelee1992 opened 6 months ago

joelee1992 commented 6 months ago

Which Cloudflare product(s) does this pertain to?

Pages

What version(s) of the tool(s) are you using?

3.17.1 [wrangler], 3.8.2 [nuxt], 8.0.6 [mailgun.js]

What version of Node are you using?

21.2.0

What operating system are you using?

Windows

Describe the Bug

I have a issue related to mailgun.js. I tried to send an email locally, and it works perfectly. However, on Cloudflare Page, it returns a status code 500 and an error message: 'Cannot convert object to primitive value.' Could you help me identify which part is causing the issue?

here is my code:

import Mailgun from 'mailgun.js'
import FormData from 'form-data'

export default defineEventHandler(async (event: any) => {
  const mailgun = new Mailgun(FormData)

  const mg = mailgun.client({ username: 'api', key: process.env.MAILGUN_KEY || '' })

  const body = await readBody(event)

  const data = {
    from: `${body.name} <${body.email}>`,
    to: process.env.MAIL_TO,
    subject: body.subject,
    text: body.message,
    html: `<html><body><p>Name : ${body.name}</p><p>Email : ${body.email}</p><p>Phone : ${body.phoneNumber}</p><p>Message : ${body.message}</p></body></html>`
  }

  return mg.messages
    .create(process.env.MAIL_DOMAIN || '', data)
    .then((response: any) => {
      return response
    })
    .catch((err: any) => {
      throw createError({
        statusCode: err
      })
    })
})

and here is my package.json

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "NITRO_PRESET=cloudflare-pages nuxt build",
    "dev": "nuxt dev --port=3002",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare",
    "pages:dev": "wrangler pages dev --compatibility-date=2023-11-21 --proxy 3000 -- npm run dev",
    "pages:deploy": "npm run build && wrangler pages deploy ./dist"
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.23.3",
    "@babel/preset-env": "^7.23.3",
    "@nuxt/devtools": "latest",
    "@nuxtjs/axios": "^5.13.6",
    "@nuxtjs/eslint-config-typescript": "^12.1.0",
    "@nuxtjs/eslint-module": "^4.1.0",
    "@types/js-cookie": "^3.0.6",
    "eslint": "^8.54.0",
    "eslint-plugin-nuxt": "^4.0.0",
    "eslint-plugin-vue": "^9.18.1",
    "eslint-plugin-vuetify": "^2.1.0",
    "form-data": "^4.0.0",
    "js-cookie": "^3.0.5",
    "libphonenumber-js": "^1.10.51",
    "nuxt": "^3.8.2",
    "nuxt-security": "^0.14.4",
    "sass": "^1.69.5",
    "vite-plugin-eslint": "^1.8.1",
    "vite-plugin-vuetify": "^1.0.2",
    "vue": "^3.3.8",
    "vue-router": "^4.2.5",
    "vuetify": "^3.4.3",
    "wrangler": "^3.17.1"
  },
  "dependencies": {
    "@mdi/font": "^7.3.67",
    "@stripe/stripe-js": "^2.2.1",
    "@types/cookie-parser": "^1.4.6",
    "@types/graylog2": "^0.2.5",
    "dotenv": "^16.3.1",
    "graylog2": "^0.2.1",
    "mailgun.js": "^8.0.6",
    "vee-validate": "^4.12.2",
    "vue-recaptcha": "^2.0.3",
    "vuex": "^4.0.2"
  }
}

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

KnoerleMan commented 1 month ago

Currently running in exactly the same issue. @joelee1992 did you find a solution to this problem?

RamIdeas commented 2 weeks ago

It depends how you're running it locally: if with nuxt (like younpm run dev/preview), then your Pages app is running in node.js and this could be a node compat issue with Cloudflare's runtime (workerd); if with wrangler pages dev (like npm run pages:dev), then there is some other discrepancy with our local and deployed environments which we'll look to fix.

Can you confirm how you ran it locally please?

boehs commented 2 weeks ago

Also experiencing with SES

Edit: checking out https://github.com/cloudflare/workers-sdk/issues/2081