2016-06-27 74 views
-2

我有一個箭頭的功能,但是當我把它,說明它不是一個函數。箭頭功能不起作用?

TypeError: callback is not a function 

module.exports = app => { 
    const mailer = nodemailer.createTransport(sgTransport(options)); 
    return { 
     send: (config, callback) => { 
      if(config.content && config.to && config.subject) { 
       let email = { 
        from: '[email protected]', 
        to: config.to, 
        subject: config.subject, 
        text: 'config.subject' 
       }; 
       const path = "./emails/" + config.template + ".html"; 
       let pageHtml = ''; 
       fs.readFile(path, (err, data) => { 
        if (err) throw err; 
        pageHtml = data.toString(); 
        email.html = pageHtml.replace("[EMAIL.CONTENT]", email.content); 

        mailer.sendMail(email, (err, res) => { 
         if (err) { 
          console.log("Erro ao enviar email.") 
         } 
         if (typeof callback == "function") { 
          return callback(res); 
         } 
        }); 
       }); 
      } 
      else { 
       console.log("Necessário passar um config válido."); 
      } 
     } 
    }; 
}; 

mail.send(email, (callback) => { 
      console.log(typeof callback); 
      // console.log(callback); 
     }); 

我是新箭頭的功能,也許這可能是問題。但是我已經搜索並找不到問題。

+1

錯誤是由哪條線產生的? –

+1

*最小*示例? http://stackoverflow.com/help/mcve –

+0

@m_callens /home/daniel/Documentos/telemonitoramentonode/api-fullmonitoramento/node_modules/mongodb/lib/utils.js:98 process.nextTick(function(){throw err; }); 該行是錯誤將無濟於事,因爲它來自nodejs事件循環(我認爲)。 –

回答

0

將其分解爲兩個步驟。定義callbalck然後導出它。

var callback = (app) => { 
    // your code 
} 
module.exports = callback; 

或者它可能是您的環境不支持ES6的原因。你需要一個vanilla ES5函數語法。

module.exports = function(app){ 
    // your code 
} 
+0

同樣錯誤:TypeError:回調不是函數 –

+0

@brubdedsbrindeds我更新了我的答案。我認爲,ES6在您的環境中不受支持。無論如何,使用簡單的函數,它將工作 –

+0

@ZohaibIjaz:如果箭頭函數不被支持,它將是一個SyntaxError,而不是一個TypeError。 –