如何在nodejs中传递db.all中的变量

时间:2018-01-08 17:44:23

标签: node.js nodemailer

我遇到了问题。我想知道是否有办法将变量req传递给db.all函数?

app.get('/sendMail', (req, res) => {
    var sql="SELECT email FROM dentists WHERE id="+req.param('id');
    db.all(sql,function(err,rows){
        if (err) 
            res.render('reponse', { err:err});
        else
            mailOptions={
                from: 'toothFairy.noreply@gmail.com',
                to: rows[0].email,
                subject: 'New message from '+req.param('firstname')+' '+req/param('lastname'),
                text: req.param('message')
            };
    });
    res.render('home');
})

1 个答案:

答案 0 :(得分:0)

是的,您可以访问,因为db.all内部的回调无需作为参数传递,您可以直接访问

app.get('/sendMail', (req, res) => {
        var sql="SELECT email FROM dentists WHERE id="+req.param('id');
        db.all(sql,function(err,rows){
            if (err) 
                res.render('reponse', { err:err});
            else
                mailOptions={
                    from: 'toothFairy.noreply@gmail.com',
                    to: rows[0].email,
                    subject: 'New message from '+req.param('firstname')+' '+req.param('lastname'),
                    text: req.param('message')
                };
        });
        res.render('home');
    })