2017-04-23 12 views
1

UIのボタンをクリックしてwebixアプリケーションから電子メールを送信し、ajax呼び出しによる投稿要求を送信します。バックエンドのノードJSサーバ。 webix部分は以下のようになります。バックエンドでノードJSサーバーのsendmailを使用してWebixアプリケーションから電子メールを送信する方法

{ id:'tb', 
    view: 'toolbar', 
    cols: [ 

    {view:"button", id:"mail_btn", type:"icon", label:"SendEmail", tooltip:"Send an email", width:100, on: {onItemClick:function(){sendEmail()}} },  
     ] 
} 

コールバック関数:

function sendEmail() {   
    var bodypart = {"message" : "This is a test mail"};   
    $.ajax({ 
      type: 'POST', 
      url: '/appl/email', 
      data: bodypart, 
      success: function (data) { 
         console.log("success"); 
        }, 
       error: function(err){ 
        console.log(err); 
        } 
       }); 
    } 
} 

上記のAJAX呼び出しは、私はこれを達成するためにsendmailのNPMパッケージを使用していたノードのJSに要求を送信します。コードは以下のようになります。しかし

var sendmail = require('sendmail')(); 

app.post('/appl/email', sendmail()); 

    function sendEmail() { 
     sendmail({ 
     from: '[email protected]', 
     to: '[email protected]', 
     subject: 'test sendmail', 
     html: 'Mail of test sendmail ', 
     }, function(err, reply) { 
     console.log(err && err.stack); 
     console.dir(reply); 
    }); 

    } 

、私はエラーの下に取得しています:

Error: Route.post() requires callback functions but got a [object Undefined] 

JSサーバをノードに要求を送信することなく、それ自体webixから電子メールを送信する方法はありますか? それ以外の方法でsendmailのnpmパッケージを使ってこれを達成する方法は?

ご協力いただければ幸いです。

答えて

1

あなたの問題はsendmailを使用しているのではなく、あなたが急行ルートを使用しているかのようになります。

ここでは、私はあなたのコードに入っているのと同じエラーを私に与えてくれたサンプルコードです。

const express = require('express'); 
const app = express(); 

app.get('/', doSomething()); 

function doSomething() { 
    console.log('this is a sample test'); 
} 

app.listen(3000,() => console.log('server is running')); 

問題はそのapp.getあり、そしてapp.postのために真であるのと同じで、それが必要とする特定の署名を持っています。渡される関数は、reqresの引数を持つはずです。オプションで最後にnext引数を追加することもできます。

これは私の上記のコードが修正される方法です。

const express = require('express'); 
const app = express(); 

app.get('/', (req, res) => { 
    doSomething(); 
    res.json('success'); 
}); 

function doSomething() { 
    console.log('this is a sample test'); 
} 
+0

間違いを指摘してくれてありがとう。私はそれを訂正し、それは今働いています。 –

関連する問題