ReferenceError: transporter is not defined

ReferenceError: transporter is not defined

我是 React js 的新手,当我尝试 运行 我的节点 js 服务器时遇到错误 我的标题框中提到了错误,请尽快修复。

index.js

这是我的 index.js 文件,我在其中编写了所有后端代码

let express = require('express');
let app = express();
const path = require('path');
let nodemailer = require('nodemailer');

// Static folder

    app.use('/public', express.static(path.join(__dirname, 'public')));
    
    nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: "harsalpatil512@gmail.com",
        pass: "*********" 
      }
    });
    
    // verifying the connection configuration
    transporter.verify(function(error, success) {
      if (error) {
        console.log(error);
      } else {
        console.log("Server is ready to take our messages!");
      }
    });
    
    
    router.post('/access', (req, res, next) => {
      var name = req.body.name
      var email = req.body.email
      var message = req.body.message
      var content = ` name: ${name} \n email: ${email} \n message: ${message} `
    
      var mail = {
        from: "harsalpatil512@gmail.com", 
        to: "ashishnirvikar5670@gmail.com", 
        message: "Welcome to Gmail",
        text: "Thanks for contacting us"
      }
    
      transporter.sendMail(mail, (err, data) => {
        if (err) {
          res.json({
            status: 'fail'
          })
        } else {
          res.json({
           status: 'success'
          })
        }
      })
    })
    
    
    const PORT = process.env.PORT || 8080
    app.listen(PORT, () => console.info(`server has started on ${PORT}`))

你需要做一点改变:

 // need to declare transporter first 
let transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: "harsalpatil512@gmail.com",
        pass: "*********" 
      }
    });