2016-06-27 19 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

* minimal * example? 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

これを2つのステップに分割します。 callbalckを定義してエクスポートします。

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

それとも、あなたの環境がES6をサポートしていない理由ができます。バニラES5関数の構文が必要です。

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

同じエラー:TypeError:コールバックは関数ではありません –

+0

@brubdedsbrindeds私は自分の答えを更新しました。 ES6はあなたの環境ではサポートされていないと思います。どのような場合でも、単純な関数を使用すると動作します –

+0

@ZohaibIjaz:矢印関数がサポートされていない場合、TypeErrorではなくSyntaxErrorになります。 –

関連する問題