程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Send Grid Email Api 发送电子邮件 3 次大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Send Grid Email Api 发送电子邮件 3 次?

开发过程中遇到Send Grid Email Api 发送电子邮件 3 次的问题如何解决?下面主要结合日常开发的经验,给出你关于Send Grid Email Api 发送电子邮件 3 次的解决方法建议,希望对你解决Send Grid Email Api 发送电子邮件 3 次有所启发或帮助;

我正在使用发送网格电子邮件 API 来发送电子邮件以验证用户帐户。出于某种原因,每次生成 3 封电子邮件而不是一封,每封电子邮件都有不同的令牌。我正在使用相同的方法来生成令牌并发送电子邮件以重置密码,而且效果很好。但是电子邮件验证令牌被发送了 3 次。

这段代码是next.js API。以下是我的代码:

import nc from "next-connect";
import crypto from "crypto";
import sgMail from "@sendgrID/mail";
import { connectToDatabase } from "../../../util/mongodb";

export const config = {
  API: {
    externalResolver: true,},};
const handler = nc().post(async (req,res) => {
  const { email } = req.body;
  const { db } = await connectToDatabase();
      
  // //TTL Index for auto expiry,will be executed only once for index creation
  // db.collection("tokens-email").createIndex(
  //   { createdAt: 1 },//   { expireAfterSeconds: 3600 } //1 hour in seconds
  // );

  db.collection("users").findOne({ email },(error,user) => {
    if (error)
      return res
        .status(500)
        .Json({ error: "an unkNown error occured,please try again" });
    if (!user) return res.status(400).Json({ error: "user doesnt exist" });
    if (user.isverifIEd)
      return res
        .status(200)
        .Json({ success: "This account has already been verifIEd" });

    const { name } = user;

    //check if token already exists for this user;
    db.collection("tokens-email").findOne({ email },token) => {
      if (error)
        return res
          .status(500)
          .Json({ error: "an unkNown error occured,please try again" });

      //if token already exists,delete this token.
      if (token)
        db.collection("tokens-email").deleteOne({ email },(error) => {
          if (error)
            return res
              .status(500)
              .Json({ error: "an unkNown error occured,please try again" });
        });

      //Create new token and save in db
      const newToken = crypto.randomBytes(128).toString("hex");

      db.collection("tokens-email").insertOne(
        {
          createdAt: new Date(),token: newToken,email,(error) => {
          if (error)
            return res
              .status(500)
              .Json({ error: "there was an unkNown issue,please try again" });

          sgMail.setAPIKey(process.env.SENDGRID_API_KEY);
          const msg = {
            to: email,from: "bm@basitminhas.com",subject: "Saqee's Online Store",text:
              "Hello " +
              name +
              ",\n\n" +
              "Please verify your account by clicking the link:" +
              process.env.CLIENT_URL +
              "API/confirm/" +
              newToken +
              "\n\nThank You!\n",};
          sgMail
            .send(msg)
            .then(() =>
              res.status(200).Json({
                success:
                  "A verification email has been sent to " +
                  email +
                  ". It will  expire after one day. If you dIDn't get verification Email click on resend token.",})
            )
            .catch(() =>
              res
                .status(500)
                .Json({ error: "technical issue,please click on resend" })
            );
        }
      );
    });
  });
});

export default handler;

谁能告诉我为什么会发生这种情况以及解决方案是什么??

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Send Grid Email Api 发送电子邮件 3 次全部内容,希望文章能够帮你解决Send Grid Email Api 发送电子邮件 3 次所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。