bugfix> sql > 投稿

問題があります。 sequelize.jsが適切にわかりません。 次のようなHTMLフォームがあります。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Forget Password</title>
</head>
<body>
Forget Password: <br/>
<form action="/forgetpasswordpartner" method="POST">
    Enter PartnerID: 
    <input type="text" name="partnerid">
    <button type="submit" value="Send Email">Send Email</button>
</form>
</body>
</html>

私はコントローラを持っています:

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, function (err, result) {
    if (err) throw err;
    else {
        res.send('Email not found in database');
    }
}, function (err, result) {
    if (err) throw err;
    else {
        let transporter = nodemailer.createTransport({
            host: 'xxxx',
            port:xxx,
            secure: false, // true for 465, false for other ports
            auth: {
                user: 'xxxxxx@xxxx.com', // generated ethereal user
                pass: 'xxxxxxxxx' // generated ethereal password
            },
            //if email is sent via localhost
            tls: {
                rejectUnauthorized: false
            }
        });
        let mailOptions = {
            from: '"xxxxxx" <xxxxxxx@xxxxx.com>', // sender address
            to: email, // list of receivers
            subject: 'Reset Password', // Subject line
            text: 'A password reset for your account was requested', // plain text body
            html: "Please click the button below to change your password.<br><a href=" + link + ">" + "<h2>Change your password</h2>" + "</a> "
            // html body
        };
        // send mail with defined transport object
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                res.send('Couldnot send Reset Password email.Please try again later');
                return console.log(error);
            }
            else {
                console.log('Message sent: %s', info.messageId);
                // Preview only available when sending through an Ethereal account
                console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
                // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
                // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
            }
        });
        res.send('To Change your password, a link has been sent to your email.Check your mail');
    }
})
}

そして、これは私のモデルです:

exports.sendresetemailpartner=function(partnerid,token,callback1,callback2){
partner.findOne({where:{'partnerid':partnerid}}).then(partner.update({'token':token},{where:{'companyemail':email}})).then((partner)=>{
    if(!partner){
        console.log('Email not registered');
        callback1();
    }
    else{
        console.log('Token Updated');
        callback2();
    }
})

私のモデルが間違っていることを知っています 私が欲しいのは、最初に「partnerid」がデータベースに存在するかどうかを確認し、存在する場合は、同じ行レコードにトークンを挿入し、そのパートナーIDの電子メールを取得し、「email」変数に保存して送信するためにそのemailidをコントローラーに送り返します郵便物

誰でも私を助けることができます。

回答 1 件
  • 電子メールの送信には別のjsファイルを使用します。メールには他の方法でもアクセスされるため。 2つのパラメーターを受け入れるジェネリックとして記述します。

    //utils.js
    module.exports = {
        sendEmail: function(toEmail, link) {
            return new Promise(function(resolve, reject) {
                let transporter = nodemailer.createTransport({
                    host: 'xxxx',
                    port: xxx,
                    secure: false, // true for 465, false for other ports
                    auth: {
                        user: 'xxxxxx@xxxx.com', // generated ethereal user
                        pass: 'xxxxxxxxx' // generated ethereal password
                    },
                    //if email is sent via localhost
                    tls: {
                        rejectUnauthorized: false
                    }
                });
                let mailOptions = {
                    from: '"xxxxxx" <xxxxxxx@xxxxx.com>', // sender address
                    to: toEmail, // list of receivers
                    subject: 'Reset Password', // Subject line
                    text: 'A password reset for your account was requested', // plain text body
                    html: "Please click the button below to change your password.<br><a href=" + link + ">" + "<h2>Change your password</h2>" + "</a> "
                    // html body
                };
                // send mail with defined transport object
                transporter.sendMail(mailOptions, (error, info) => {
                    if (error) {
                        res.send('Couldnot send Reset Password email.Please try again later');
                        //return console.log(error);
                        reject(error)
                    } else {
                        console.log('Message sent: %s', info.messageId);
                        // Preview only available when sending through an Ethereal account
                        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
                        // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
                        // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
                        resolve("success")
                    }
                });
            });
        }
    }
    
    //Require util.js
    import Utils from '../utils'
    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;
        partner.findOne({
            where: {
                'partnerid': partnerid
            }
        }).then((fetchedPartner) => {
            fetchedPartner.update({
                token: token
            }).then((postUpdate) => {
                if (!postUpdate) {
                    console.log('Email not registered');
                } else {
                    Utils.sendEmail('xyz@gmail.com', link).then((postEmail) => {
                        // your logic for success
                    });
                }
            });
        });
    }
    
    

あなたの答え