TypeError:无法读取属性' schoolName'未定义的

时间:2018-02-01 15:36:24

标签: javascript node.js mongoose nodemailer

所以我试图删除用户填写的表单,提交时用户绑定到user.id但是当我尝试调用某些用户属性时,我得到一个未定义的错误。

这是我的路线:

// delete competition form
router.get('/dashboard/users/forms/competition-form/delete/:id', ensureAuthenticated, (req, res, next) => {
  CompetitionForm.findByIdAndRemove(req.params.id, function(err, competition, user){
    req.flash('success_msg', `The competition form removed successfully!`);
    res.redirect('/dashboard');

    const output = `
      <h3>Competition Form Deletion</h3>
      <p>Hello ${user.schoolName},<p>
      <p>We are emailing you to make you aware that your competition form has been deleted from our records!<p>
      <br>
      <p>- POL – Puerto Rico</>
    `;

    nodemailer.createTestAccount((err, account) => {
      // create reusable transporter object using the default SMTP transport
      if (process.env.NODE_ENV === 'production') {
        transporter = nodemailer.createTransport({
          host: "smtp.sendgrid.net",
          port: 587,
          auth: {
            user: process.env.SENDGRID_USERNAME,
            pass: process.env.SENDGRID_PASSWORD,
          }
        });
      } else {
        transporter = nodemailer.createTransport({
          host: "smtp.ethereal.email",
          port: 587,
          auth: {
            user: 'qkkvnabtziufbksa@ethereal.email',
            pass: 'A4W9HF2WbhAav263VM',
          }
        });
      }
      // setup email data with unicode symbols
      let mailOptions = {
        from: process.env.GLOBAL_EMAIL || 'ben@benbagley.co.uk', // sender address
        to: `${user.email}`, // list of receivers
        subject: 'Competition Form Deletion | Poetry Out Loud', // Subject line
        html: output // html body
      };
      // send mail with defined transport object
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          return console.log(error);
        }

        console.log('Message sent: %s', info.messageId);
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
      });
    });
  });
});

如果此处需要任何其他代码,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:0)

您看到的错误意味着user在调用时未定义。尝试在函数debugger;中的某个位置放置function(err, competition, user){...}并在启用开发工具的情况下运行调用。当它停止时,你应该能够使用调用堆栈并找到你的问题。