NodeJS 无法设置mailgun键和用户名设置

yebdmbv4  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(155)

我的数据库中有Mailgun设置配置。所有我想要的Mailgun客户端密钥,用户名,和电子邮件应该从数据库中提取任何邮件去客户端。但我不能设置数据。from: mailgun_email,这部分也是我们从数据库中获取的。请帮助我如何设置数据。还有一件事,如果我在电子邮件文件之外调用配置文件,它会给我一个错误mongoose错误,这是我所理解的,即在数据库设置缓冲超时错误之前调用配置文件。

错误MongooseError: Operation configurations.findOne() buffering timed out after 10000ms

邮件文件

let mg;

const configuration = async () => {
    const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });

    let key = res.mailgun_api_key;
    let username = res.mailgun_username;

    return mailgun.client({
        username,
        key,
    });
};

const successOnRegisterMail = (user_email, user_name) => {
    configuration();
    try {
        mg.messages
            .create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
                from: mailgun_email,
                to: [user_email],
                subject: "",
                text: "Congratulation you've been successfully registered.",
                html: `<p>Hello ${user_name}</p> <br /><br /> <p>Congratulation you've been successfully registered.</p><br /> Best Regards, <br /> Team Developers`,
            })
            .then((msg) => console.log(msg)) // log response data
            .catch((error) => console.log(error)); // log any error
    } catch (err) {
        console.log("Error from Mail: ", err);
    }
};
uqdfh47h

uqdfh47h1#

经过多次尝试,我终于找到了答案。

const configuration = async () => {
    const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });

    const mg = mailgun.client({
        username: res.mailgun_username,
        key: res.mailgun_api_key,
    });

    if (res) {
        return {
            mg,
            email_id: res.email_id_for_mail_sent,
        };
    } else {
        return console.log("Error Unable To Fetched Data");
    }
};

在邮件里用这个

const mailgunData = await configuration();

mailgunData.mg.messages
            .create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
                from: mailgunData.email_id,
                to: [user_email],
                subject: "",

相关问题