2015-11-24 61 views
0

我正在使用Express與denodeify模塊。當我嘗試異步呈現電子郵件模板時,出現錯誤。表示承諾拋出錯誤

.then(function() { 
    return denodeify(res.render)(path.resolve('verify-email'), { 
     name: user.displayName, 
     appName: config.app.title, 
     mail: user.email, 
     url: 'http://' + req.headers.host 
    }); 
}) 
.then(function (emailHTML) { 
    // code which is not executed 
}) 
.catch(function (err) { 
    // [TypeError: Cannot read property 'req' of undefined] 
}); 

我嘗試使用res.render沒有回調,它工作正常,但我需要在變量中輸出電子郵件模板。 另外我不明白我可以如何調試這部分代碼。感謝幫助! 整個代碼

function sendVerificationEmail(user, req, res) { 
    return denodeify(crypto.randomBytes)(20) 
     .then(function (buffer) { 
      user.verificationToken = buffer.toString('hex'); 
      return user.save(); 
     }) 
     .then(function() { 
      user.password = undefined; 
      user.salt = undefined; 
      delete req.session.redirect_to; 

      return denodeify(res.render)(path.resolve('modules/users/server/templates/verify-email'), { 
       name: user.displayName, 
       appName: config.app.title, 
       mail: user.email, 
       url: 'http://' + req.headers.host + '/api/auth/verify/' + user.verificationToken + "/" + user.email 
      }); 
     }) 
     .then(function (emailHTML) { 
      const mailOptions = { 
       to: user.email, 
       from: config.mailer.from, 
       subject: 'Verify Email', 
       html: emailHTML 
      }; 
      return denodeify(smtpTransport.sendMail)(mailOptions); 
     }); 
} 

exports.updateAuthData = function (req, res, next) { 
    const userId = req.session.userId; 
    delete req.session.userId; 

    if (!userId) { 
     return res.status(401).send({ 
      message: 'No active session' 
     }); 
    } 

    let user; 

    User.findById(userId) 
     .then() 
     .then((_user) => { 
      user = _user; 

      const fields = Object.keys(req.body); 
      const neededFields = user.getMissingAuthFields(); 
      const missingFields = _.difference(fields, neededFields); 

      if (missingFields.length !== 0) { 
       throw new CustomError('Missing fields: ' + missingFields.join(', ')) 
      } 

      for (let field of User.requiredAuthorizationFields) { 
       if (!!user[field]) { 
        throw new CustomError('User already has ' + field); 
       } 
      } 

      User.requiredAuthorizationFields.forEach((field) => user[field] = req.body[field]); 

      return true; 
     }) 
     .then(function() { 
      return sendVerificationEmail(user, req, res); 
     }) 
     .then(() => { 
      res.send({ 
       message: 'An email has been sent to the provided email, please verify your account.' 
      }); 
     }) 
     .catch(function (err) { 
      res.status(400).send({ 
       message: 'Failure sending email' 
      }); 
     }) 
}; 
+0

由於錯誤提示:'req'未定義。你從哪裏需要這個變量? – Beat

+1

您可以在沒有承諾的情況下執行此操作,'res.render('verify-email',{name:....},function(err,emailHTML){res.render('index',{email:emailHTML})} ); ' – Molda

+0

請添加整個功能。 –

回答

0

我找到了解決辦法,我們必須綁定「這個」像這樣

return denodeify(res.render.bind(res))(path.resolve('modules/users/server/templates/verify-email'), { 
     name: user.displayName, 
     appName: config.app.title, 
     mail: user.email, 
     url: 'http://' + req.headers.host + '/api/auth/verify/' + user.verificationToken + "/" + user.email 
}); 

的原因錯誤的變量是快遞「res.render」下面的代碼功能

var app = this.req.app; 
    var done = callback; 
    var opts = options || {}; 
    var req = this.req; 
    var self = this; 

正確的方法是使用:

denodeify(res.render.bind(res))(path, options)

代替

denodeify(res.render)(path, options) 

感謝所有的答覆!