When I try to import a function with npm modules that I exported, I get "Class extends value undefined is not a constructor or null" in sveltekit

huangapple go评论65阅读模式
英文:

When I try to import a function with npm modules that I exported, I get "Class extends value undefined is not a constructor or null" in sveltekit

问题

我正在尝试使用 SvelteKit 和 npm 模块 "nodemailer" 在我的表单中发送带有数值的电子邮件,但当我导入该函数时,出现以下错误: "Class extends value undefined is not a constructor or null"。

这是包含该函数的 mail.js 文件:

import nodemailer from "nodemailer";

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'mymail',
        pass: 'mypass'
    }
});

export function sendEmail(text) {
    const mailOptions = {
        from: "from",
        to: "to",
        subject: "test",
        text
    }

    transporter.sendMail(mailOptions)
}

这是我的位于 /contact 文件夹中的 +page.svelte

<script>
    import Navbar from "../Navbar.svelte";
    import Footer from "../Footer.svelte";

    import {sendEmail} from "./mail.js";

    //sendEmail("message")
</script>

<Navbar/>

<form id="contact-form">
    <textarea id="message" cols="30" rows="10"></textarea>
    <button type="submit">Send</button>
</form>

<Footer/>

奇怪的事情发生在当我访问页面时,出现 500 错误,并附有我提供的描述,但当取消注释 sendEmail() 函数时,它实际上会发送邮件。

我想要使用表单中的数值发送我的邮件。

英文:

I am trying to send emails with values in my form with sveltekit using npm module: "nodemailer", but when I import the function I have I get this error: "Class extends value undefined is not a constructor or null"

This is the mail.js with the function:

import nodemailer from &quot;nodemailer&quot;

var transporter = nodemailer.createTransport({
    service: &#39;gmail&#39;,
    auth: {
        user: &#39;mymail&#39;,
        pass: &#39;mypass&#39;
    }
});

export function sendEmail(text) {
    const mailOptions = {
        from: &quot;from&quot;,
        to: &quot;to&quot;,
        subject: &quot;test&quot;,
        text
    }

    transporter.sendMail(mailOptions)
}

and this is my +page.svelte in /contact folder:

&lt;script&gt;
    import Navbar from &quot;../Navbar.svelte&quot;;
    import Footer from &quot;../Footer.svelte&quot;;

    import {sendEmail} from &quot;./mail.js&quot;;

    //sendEmail(&quot;message&quot;)
&lt;/script&gt;

&lt;Navbar/&gt;

&lt;form id=&quot;contact-form&quot;&gt;
    &lt;textarea id=&quot;message&quot; cols=&quot;30&quot; rows=&quot;10&quot;&gt;&lt;/textarea&gt;
    &lt;button type=&quot;submit&quot;&gt;Send&lt;/button&gt;
&lt;/form&gt;

&lt;Footer/&gt;

The weird stuff happens, when I go on the page, I get 500 error with the description I provided, but when the sendEmail() function is uncommented, it actually sends the email on mine.

I want to send my mail with the values from the form.

答案1

得分: 1

So after digging deep into the answer provided by Tobi Obeck, I finally got it right. I still don't know why the script has been executed, but I do understand why the page hit 500 server error everytime I tried to post the form. I am sending my code for everyone that is out there, here's my solution:

import nodemailer from "nodemailer"

var transporter = nodemailer.createTransport( {
    service: "gmail",
    auth: {
        user: "mymail",
        pass: "mypass"
    }
})

/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ request }) => {
    const data = await request.formData();

    const email = data.get("email")
    const message = data.get("message")

    const mailOptions = {
        from: email,
        to: "tomail",
        subject: `subject`,
        text: message
    }

    await transporter.sendMail(mailOptions)

    }
}

Please note that I didn't translate the code parts, as you requested.

英文:

So after digging deep into the answer provided by Tobi Obeck, I finally got it right. I still don't know why the script has been executed, but I do understand why the page hit 500 server error everytime I tried to post the form. I am sending my code for everyone that is out there, here's my solution:

import nodemailer from &quot;nodemailer&quot;

var transporter = nodemailer.createTransport( {
    service: &quot;gmail&quot;,
    auth: {
        user: &quot;mymail&quot;,
        pass: &quot;mypass&quot;
    }
})

/** @type {import(&#39;./$types&#39;).Actions} */
export const actions = {
default: async ({ request }) =&gt; {
    const data = await request.formData();

    const email = data.get(&quot;email&quot;)
    const message = data.get(&quot;message&quot;)

    const mailOptions = {
        from: email,
        to: &quot;tomail&quot;,
        subject: `subject`,
        text: message
    }

    await transporter.sendMail(mailOptions)

    }
}

huangapple
  • 本文由 发表于 2023年3月31日 17:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定