我想通过单击 UI 中的按钮从 webix 应用程序发送电子邮件,这将通过 ajax 调用向后端的 Node JS 服务器发送 post 请求。 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 调用向 Node JS 发送一个请求,我在其中使用 sendmail npm 包来实现此目的。代码如下所示:
var sendmail = require('sendmail')();
app.post('/appl/email', sendmail());
function sendEmail() {
sendmail({
from: 'xyz@support.com',
to: 'abc@support.com',
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]
有没有办法从 webix 本身发送电子邮件而不将请求发送到 Node JS 服务器? 或者如何使用 sendmail npm 包以我正在尝试的方式实现此目的?
如有任何帮助,我们将不胜感激。
最佳答案
您的问题不在于您使用 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
也有同样的情况,有它需要的特定签名。传入的函数应该具有 req
和 res
参数。您还可以选择在最后添加 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');
}
关于javascript - 如何使用后端 Node JS 服务器的 sendmail 从 Webix 应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574901/