TypeError:无法读取undefined的属性'schoolName'

时间:2022-12-02 02:34:28

So I'm trying to delete a form that is filled in by a user, the user when submitted is tied into the user.id however when I try to call some of the user properties I get an undefined error.

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

Here is my route:

这是我的路线:

// 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));
      });
    });
  });
});

If any other code is needed here, do let me know.

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

Thanks.

谢谢。

1 个解决方案

#1


0  

The error you are seeing means that user is undefined when it is called. Try placing a debugger; somewhere in the function function(err, competition, user){...} and running the call with dev tools open. When it stops you should be able to use the call stack and find your problem.

您看到的错误意味着用户在调用时未定义。尝试放置调试器;功能函数中的某个地方(错误,竞争,用户){...}并使用dev工具打开调用。当它停止时,您应该能够使用调用堆栈并找到您的问题。

#1


0  

The error you are seeing means that user is undefined when it is called. Try placing a debugger; somewhere in the function function(err, competition, user){...} and running the call with dev tools open. When it stops you should be able to use the call stack and find your problem.

您看到的错误意味着用户在调用时未定义。尝试放置调试器;功能函数中的某个地方(错误,竞争,用户){...}并使用dev工具打开调用。当它停止时,您应该能够使用调用堆栈并找到您的问题。