You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Sequelize.js:如何根据用户输入的PartnerID获取邮箱并返回至控制器?

Fixing Your Sequelize Password Reset Flow

Let's work through fixing your model code first—this is where the main issues are preventing you from checking the PartnerID, updating the token, and passing the email back to your controller.

What's Wrong With the Original Model Code

Your current model has a few critical issues:

  • The chained then() after findOne is miswritten—you're calling partner.update() directly instead of using the instance returned by findOne
  • The email variable in your update call is undefined, so the update won't target the right row
  • You aren't passing the found partner's email back to the controller, which means your email-sending logic can't use it
  • Callback handling is inconsistent, with no way to pass data between the model and controller

Corrected Model Code

Here's the fixed model code that addresses all these issues:

exports.sendresetemailpartner = function(partnerid, token, callback1, callback2) {
  // First, find the partner by their partnerid
  partner.findOne({ where: { 'partnerid': partnerid } })
    .then(foundPartner => {
      if (!foundPartner) {
        // No partner found with this ID—call the failure callback
        console.log('PartnerID not registered');
        callback1();
        return;
      }

      // Update the token for the found partner
      return foundPartner.update({ 'token': token })
        .then(updatedPartner => {
          // Pass the partner's email to the success callback
          console.log('Token Updated');
          callback2(null, updatedPartner.companyemail);
        });
    })
    .catch(err => {
      // Handle any database errors and pass them to the callbacks
      callback1(err);
    });
};

Key Changes Explained:

  1. Proper Instance Handling: We use the foundPartner instance returned by findOne to call update()—this ensures we're updating exactly the row we found.
  2. Email Passing: After updating, we send the companyemail from the updated partner to the controller via callback2.
  3. Error Handling: Added a catch() block to handle database errors and pass them back to the controller instead of silently failing.
  4. Clear Flow: Explicitly check if the partner exists first, then proceed with the update only if they do.

Updated Controller Code

Now you need to adjust your controller to receive the email from the model and use it in your nodemailer setup:

exports.sendresetlinkpartner = function (req, res, next) { 
  var partnerid = req.body.partnerid; 
  var token = randomstring.generate({ 
    length: 100, 
    charset: 'alphanumeric', 
    capitalization: 'uppercase' 
  }); 
  var link = "http://localhost:3000/forgetpasswordpartner/" + token; 

  forgetpasswordmodel.sendresetemailpartner(partnerid, token, 
    // Failure callback (no partner found)
    function (err) { 
      if (err) {
        console.error(err);
        return res.send('An error occurred. Please try again later.');
      }
      res.send('PartnerID not found in database'); 
    }, 
    // Success callback (partner found, email passed here)
    function (err, email) { 
      if (err) {
        console.error(err);
        return res.send('An error occurred. Please try again later.');
      }

      let transporter = nodemailer.createTransport({ 
        host: 'xxxx', 
        port: xxx, 
        secure: false, 
        auth: { 
          user: 'xxxxxx@xxxx.com', 
          pass: 'xxxxxxxxx' 
        }, 
        tls: { rejectUnauthorized: false } 
      }); 

      let mailOptions = { 
        from: '"xxxxxx" <xxxxxxx@xxxxx.com>', 
        to: email, // Now using the email passed from the model
        subject: 'Reset Password', 
        text: 'A password reset for your account was requested', 
        html: `Please click the button below to change your password.<br><a href="${link}"><h2>Change your password</h2></a>` 
      }; 

      transporter.sendMail(mailOptions, (error, info) => { 
        if (error) { 
          console.log(error);
          return res.send('Could not send Reset Password email. Please try again later'); 
        } 
        console.log('Message sent: %s', info.messageId); 
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); 
        res.send('To change your password, a link has been sent to your email. Check your mail'); 
      }); 
    }
  ); 
};

Key Controller Adjustments:

  • The success callback now accepts an email parameter, which we get from the model.
  • We use this email value in mailOptions.to so the reset email is sent to the correct address.
  • Added error handling in both callbacks to catch database errors and inform the user properly.

内容的提问来源于stack exchange,提问作者Bibek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:45:10