2017-08-16 51 views
0

我想通過郵件以表格格式發送響應。 在郵件選項html的我宣佈res.json(streamingTemplate)==並返回以下錯誤:如何在node.js中發送郵件通知以獲取響應

Unhandled rejection Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) at ServerResponse.header

一樣,我有......

var sendStreamingTemplate = function (req, res) { 

authToken = req.headers.authorization; 
userAuthObj = JSON.parse(UserAuthServices.userAuthTokenValidator(authToken)); 
var todayDate = new Date(); 
var expireDate = new Date(userAuthObj.expire_date); 
tokenOK = TokenValidator.validateToken(userAuthObj.user_id, authToken).then(function (userSessions) { 
    if (userSessions.length === 1) { 
     if (expireDate >= todayDate) { 
      StreamingTemplateId = req.params.id; 

      TemplateController.findById(StreamingTemplateId).then(function (streamingTemplate) { 
       if (streamingTemplate === null) { 
        res.status(404).json({ 
         message: 'Streaming not found...' 
        }) 
       } else { 
        console.log(streamingTemplate); 

        let transporter = nodemailer.createTransport({ 
        host: 'smtp.zoho.com', 
        port: 465, 
        auth: true, 
        active: true, 
        secure: true, 
        requireTLS: true, 
        auth: { 
         user: '[email protected]', 
         pass: 'yyyyyyyy' 
          } 
         }); 

        let mailOptions = { 
        from: '[email protected]', 
        to: '[email protected]', 
        subject: 'mail notification Test', 
        text: 'Hello !!!!!!!!!everything works fine', 
        html:res.json(streamingTemplate)// i want to send whatever response get i will send to through mail in table format 
         }; 

        transporter.sendMail(mailOptions, (error, info) => { 
        if (error) { 
         return console.log("mail not sent" +error.message); 
          } 
         console.log('success'); 
         }); 

        switch(streamingTemplate.template_name.toString().toLowerCase()){ 
          case "stream": 
          res.json(streamingTemplate); 
          break; 
          case "broadcaster": 
          res.json(streamingTemplate); 
          break; 
          case "product": 
          res.json(streamingTemplate); 
          break; 
          default: 
          res.json(streamingTemplate); 
          break; 
         } 
       } 
      }).catch(function (err) { 
       res.status(500).json({ 
        message: 'something went wrong...' 
       }); 
      }); 
     } else { 
      res.status(401).json({ 
       message: 'Not Authorized...' 
      }); 
     } 
    } else { 
     res.status(401).json({ 
      message: 'Token Expired...' 
     }); 
    } 
}).catch(function (err) { 
    res.status(401).json({ 
     message: 'Token Expired...' 
    }); 
}); 
}; 
+0

有人能幫我 –

回答

0

的res.json()方法是一個終端方法,它會結束寫入其中指定的json的響應。因此,在配置mailOptions時,不應使用它。

let mailOptions = { 
      from: '[email protected]', 
      to: '[email protected]', 
      subject: 'mail notification Test', 
      text: 'Hello !!!!!!!!!everything works fine', 
      html: res.json(streamingTemplate) // Response should not be sent here 
     }; 

相反,streamingTemplate應該傳遞給mailOptionsHTML說法。

let mailOptions = { 
      from: '[email protected]', 
      to: '[email protected]', 
      subject: 'mail notification Test', 
      text: 'Hello !!!!!!!!!everything works fine', 
      html: streamingTemplate // set the template here 
     }; 
+0

我試過錯誤只來抓我 –

+0

是這一個 –

+0

任何其他方法的任何其他的想法是有發送通知正從MySQL的響應,並以HTML表格形式通知 –

相關問題