Jump to content

Facing Issue Fetching User Email for Profit Notification in Crypto Exchange


Recommended Posts

Hello,

I’m developing a crypto exchange and trading platform and have added a feature where users are congratulated via email when they make a 100% profit on their investment. However, I’m facing an issue fetching the user’s email ID to send the congratulatory email. Even though the user data is correctly saved in the database, the email fetching logic seems to be failing.

Here’s a snippet of the code I’m using to fetch the user’s email and send the congratulatory email:

 
const nodemailer = require("nodemailer");

app.post("/check-profit", async (req, res) => {
    try {
        const { userId, cryptoCurrency, initialInvestment, currentValue } = req.body;
        const user = await User.findById(userId);
        
        if (!user) {
            return res.status(404).json({ error: "User not found" });
        }
        
        // Calculate profit percentage
        const profitPercentage = ((currentValue - initialInvestment) / initialInvestment) * 100;
        
        if (profitPercentage >= 100) {
            // Send congratulatory email
            const transporter = nodemailer.createTransport({
                service: "gmail",
                auth: {
                    user: "[email protected]",
                    pass: "your-email-password"
                }
            });
            
            const mailOptions = {
                from: "[email protected]",
                to: user.email, // Issue: Not fetching email
                subject: "Congratulations on Your 100% Profit!",
                text: `Dear ${user.name},\n\nCongratulations on reaching 100% profit in ${cryptoCurrency}!\nKeep up the great work!`
            };
            
            transporter.sendMail(mailOptions, (error, info) => {
                if (error) {
                    console.log("Error sending email:", error);
                    return res.status(500).json({ error: "Error sending email" });
                }
                console.log("Email sent: " + info.response);
            });
            
            res.status(200).json({ message: "Congratulatory email sent" });
        } else {
            res.status(200).json({ message: "No profit to congratulate" });
        }
    } catch (error) {
        console.error("Error checking profit:", error);
        res.status(500).json({ error: "Internal Server Error" });
    }
});

 

Steps I've Tried:

  • Verified that user.email exists and is correctly stored in the database.
  • Logged user.email before sending the email, but it’s coming as undefined.
  • Checked the schema and it looks like the email is present in the database.

Has anyone faced a similar issue when trying to fetch user email from the database in a Node.js app? Any guidance on resolving this issue would be really helpful.

Thanks in advance!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 👍 Join TopGold.Forum Now

    Join The Most Welcoming Crypto & Trading Community

    We are over 25,000 members and 700 companies on our journey to strike GOLD.💰

    👩 Want to make money online? 
    💼 Represent a company? 

⤴️-Paid Ad- TGF approve this banner. Add your banner here.🔥

×
×
  • Create New...