我已经用Koa2设置了一个应用程序接口,它在发布到/contact时向mailgun发送一个带有名称、电子邮件和消息的请求。电子邮件发送得很好,但我在控制台客户端得到了404。为什么我会有这样的行为?
module.exports = {
/**
*@api {post} /contact
*@apiGroup Public
* @apiName sendMail
* @apiParam {String} [name] User needs to provide their name
* @apiParam {String} [email] User needs to provide their email
* @apiParam {String} [message] user needs to provide their message
* @apiParamExample {String} Request Params :
* {
* "name" : "Cello",
* "email" : "yo@yo.ma,
* "message" : "you are so great"
* }
* @apiSuccess {Object} return value A success or failure
* @apiSuccessExample {json} Response:
* {
* "result" : "success"
* "callback" : "Do something"
* }
* @apiExample {curl} Example usage:
* curl -i http://localhost:4000/contact
* @apiDescription Any user can submit contact
* @apiHeader {String} Authorization JWT Authorization header (optional)
* @apiHeaderExample {json} Request Authorization Header
* {
* "authorization" : "jkahdkjashdk324324342"
* }
*/
async mail(ctx){
let {email, name, message} = ctx.request.body;
if (!email) {
ctx.throw(400, 'please provide the email')
}
if (!name) {
ctx.throw(400, 'please provide a name')
}
if (!message) {
ctx.throw(400, 'please provide a message')
}
var api_key = 'key-xxxxxxxxxx';
var domain = 'mg.domain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var data = {
from: name+'<'+email+'>',
to: 'me@domain.com',
subject: 'Contact Form',
text: message
};
var result = mailgun.messages().send(data, function (error, body) {
console.log(body);
});
}}
发布于 2018-06-01 20:03:43
当您使用async
-await
模式时,您的代码应该如下所示:
async mail(ctx){
...
var result = await mailgun.messages().send(data)
console.log(body);
};
缺少的部分是await
。由于文档的原因,mailgun-js
模块也实现了promises,这应该可以工作(未经过测试)。
https://stackoverflow.com/questions/49479414
复制相似问题