Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js 404发送电子邮件时出错,忽略我的资源状态_Javascript_Angularjs_Node.js_Express_Nodemailer - Fatal编程技术网

Javascript Node.js 404发送电子邮件时出错,忽略我的资源状态

Javascript Node.js 404发送电子邮件时出错,忽略我的资源状态,javascript,angularjs,node.js,express,nodemailer,Javascript,Angularjs,Node.js,Express,Nodemailer,我有一个带有表单的angular应用程序,可以发出ajax请求。发电子邮件很好,但不管我的回复是什么;我得到一个路径错误。我假设node希望路径“/send”呈现模板或数据,但我只想要电子邮件的路径!~我使用一个模式弹出窗口作为表单&路径的角度ui路由 app.get('/send', function (req, res, err) { var cartContent = req.body.slice(1,20); var content = tableify(cartContent

我有一个带有表单的angular应用程序,可以发出ajax请求。发电子邮件很好,但不管我的回复是什么;我得到一个路径错误。我假设node希望路径“/send”呈现模板或数据,但我只想要电子邮件的路径!~我使用一个模式弹出窗口作为表单&路径的角度ui路由

app.get('/send', function (req, res, err) { 
  var cartContent = req.body.slice(1,20);
  var content = tableify(cartContent);
  var sendit = {
    to: '*****@gmail.com',
    from: '****@bluenightphoto.com',
    replyTo: req.body[0].email,

    subject: "CP Carpet Quote Request: " + req.body[0].fname + " " + req.body[0].lname , // REQUIRED.

    html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " +
    '<br>' + 'Message: ' + req.body[0].message + '<br>' + <b>Table</b>'+ '<br><br>' + content,
  };

  // Transporter refers to nodemailer and sendit is the login details(mail works fine!)

  transporter.sendMail(sendit, function (req, res, err) {
    if (err) {
      console.log(err + "something strange...");
      res.status(401).send("that's all folk's");
    } else {
      res.status(200).send("nuthing here");
      console.log("Message sent! " );
    }

    transporter.close();
  });
});
console.log('确实发生了错误'+err);总是开火..也许我需要发回数据

以下是我的express代码中的错误处理程序:

if (app.get('env') === 'production') {

    // changes it to use the optimized version for production
    app.use('/', express.static(path.join(__dirname, '/dist')));

    // production error handler
    // no stacktraces leaked to user
      app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

以下是一些修复和注释:

app.get('/send', function (req, res, next) {
  // ...
  transporter.sendMail(sendit, function (req, res, next) { // Asynchronous = do it later.
    // Parent arg `res` is masked by another `res` which is passed by `sendMail`.
    // I don't know if `sendMail` passes the same one or some other `res`.
    // You may try renaming it to `res2`.
  }
  return next(); // For now call next middleware, all async code will be executed later.
我已经阅读了您的评论,但首先请确保下一部分: 从…起

在您的问题解释之上尝试此更改,我希望它能解决您的问题。:-)


这起作用了。试图在transporter函数内设置res.send对象导致其未触发。把它放在外面可以让它很好,现在我没有任何错误

您正在一点一点地进行异步,但我很确定我们缺少了一段重要的代码。路线控制器里还有其他东西吗?另外,中间件是
(req,res,next)
它是否与
angularjs
相关?如果它不符合条件,那么移除标记以隐藏它,不让渴望
angularjs
的大脑知道。我添加了angularAjax代码。你是否成功地返回了?我认为200美元还不够。请看下面我的答案。最初得到的404或500不起作用,我在原始问题中添加了错误处理程序和ajax角度代码next()我给了你赏金,你的回答指引了我正确的方向太棒了!谢谢你,我很高兴能够帮助你!:-)现在你有什么问题吗?还是一切都像你想的那样工作?没有问题,它100%解决了问题。。我很惊讶我能在运输车里工作。
app.get('/send', function (req, res, next) {
  // ...
  transporter.sendMail(sendit, function (req, res, next) { // Asynchronous = do it later.
    // Parent arg `res` is masked by another `res` which is passed by `sendMail`.
    // I don't know if `sendMail` passes the same one or some other `res`.
    // You may try renaming it to `res2`.
  }
  return next(); // For now call next middleware, all async code will be executed later.
// Transporter refers to nodemailer and sendit is the login details(mail works fine!)
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'sender@gmail.com',
        pass: 'password'
    }
});
var sendIt = { //<------ i've just renamed this compare to nodemailer.com sample 
    from: 'sender@address',
    to: 'receiver@address',
    subject: 'hello',
    text: 'hello world!'
};


// this part is very important !
// note the signature.
transporter.sendMail(sendIt, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);

});
transporter.sendMail(  sendit, function (  req, res, err)  {
//transporter.sendMail(sendit, function (error, info) {

    if (err) { 
      console.log(err + "something strange...");
      res.status(401).send("that's all folk's");

      // you are acting here on the 'info' coming from transporter.sendMail
     // not on 'res' from app.get('/send',      


    } else {
      res.status(200).send("nuthing here");
      console.log("Message sent! " );


      // you are acting here on the 'info' coming from transporter.sendMail
     // not on 'res' from app.get('/send',      

    }
transporter.sendMail(sendit,function(error, info){
if (error) {
    console.log(error + "something strange...");
    }

console.log("Message sent!");
});

    res.send("Messaage was Sent");

return next();
});