First time mail successfully send but if i send mail again then showing this error
E11000 duplicate key error collection: ShopCart.tokens index: sellerId_1 dup key: { sellerId: ObjectId(’66af901a089b822f1dc3ff7d’) }
this is my seller route post request for send email code——
router.post("/send-email", async (req, res) => {
const email = req.body.email;
const seller = await Seller.findOne({email: {$regex: '^'+email+'$', $options: 'i'}});
if(!seller) {
return res.status(400).send({ message: "Seller not found to reset the email" });
}
const payload = {
email: seller.email
}
const expiryTime = 500;
const token = jwt.sign(payload, process.env.TOKEN_SECRET, {expiresIn: expiryTime});
const newToken = new SellerToken({
sellerId: seller._id,
token: token
});
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "[email protected]",
pass: "vdzpqeillbxoxkja"
}
});
const mailDetails = {
from: '[email protected]',
to: email,
subject: 'Reset Password!',
html:
`<html>
<head>
<title>Password Reset Request</title>
</head>
<body>
<h1>Password Reset Request</h1>
<p>Dear ${Seller.name},</p>
<p>We have received a request to reset your password for your account with ShopCart. To complete the password reset process, please click on the button below: </p>
<a href=${process.env.LIVE_URL}/reset/${token}>
<button style="background-color: #4CAF50; color: white; padding: 14px 20px; border: none; cursor: pointer; border-radius: 4px;">Reset Paswword</button>
</a>
<p>Please note that this link is only valid for a 2mins. If you did not request a password reset, please disregard this message.</p>
<p>Thank you,</p>
<p>ShopCart Team</p>
</body>
</html>`,
};
transporter.sendMail(mailDetails, async(error, data) =>{
if (error) {
console.log(error);
return res.status(400).send({ message: "Something went wrong when email send" });
} else {
await newToken.save();
return res.status(200).send({ message: "Email Sent Successfully"})
}
});
});
and seller token code
// Forgot Password Code
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const tokenSchema = new Schema({
sellerId: {
type: Schema.Types.ObjectId,
required: true,
ref: "seller",
unique: true,
},
token: { type: String, required: true },
createdAt: { type: Date, default: Date.now, expires: 3600 },
});
module.exports = mongoose.model("Token", tokenSchema);
New contributor
Vishal Hariyani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.