Strapi beta 的结构改变了插件的架构方式,删除了 /plugins 目录,插件现在保存在 /node_modules 目录中。我正在尝试编写一些自定义代码以在下订单后触发确认电子邮件。在以前版本的 Strapi 中,电子邮件插件目录位于此处:
/server/plugins/email/controllers
在此目录中,编写了以下代码,该代码在 SEND 控制器的 alpha 版本中运行:
我们对此进行评论:// await.strapi.plugins.email.services.send(options, config);
然后这段代码被用在曾经存在的电子邮件控制器的 module.exports 内的 SEND 控制器中......
let options = ctx.request.body;
try {
// send email to user
await strapi.plugins['email'].services.email.send({
to: options.to,
from: '[email protected] /cdn-cgi/l/email-protection',
subject: options.subject,
text: options.text,
html: options.html
})
} catch (err) {
return ctx.badRequest(null, err);
}
// Send 200 'ok;
ctx.send({});
好吧,这就是服务器端的 Strapi Send 控制器……现在在客户端,订单承诺返回后,我们会触发另一个确认电子邮件的承诺,该电子邮件会点击 Strapi API:
await.strapi.request('POST', '/email', {
data: {
to: confirmationEmailAdress,
subject: "Order Confirmation',
text: 'Your order has been processed',
html: '<b>Expect your stuff to arrive broken. Thanks.</b>'
}
});
简而言之,现在的问题是 Strapi 的架构发生了变化,不确定将我的服务器代码放在上面的位置,以及调用以触发电子邮件的 API url。我在 Strapi 中使用 API 密钥、权限设置了 SendGrid,一切都很好,只是问题是,既然 Beta 架构已从 alpha 更改,那么放置此代码的正确位置在哪里?
* 更新代码 *
根据吉姆的建议,我现在在 /extensions 文件夹中创建了一个电子邮件控制器,如下所示:
/server/extensions/email/controllers/Email.js
Email.js
'use strict';
module.exports = {
send: async (ctx) => {
let options = ctx.request.body;
try {
//Send email to the user
await strapi.plugins['email'].services.email.send({
to: options.to,
from: '[email protected] /cdn-cgi/l/email-protection',
subject: options.subject,
text: options.text,
html: options.html
});
} catch (err) {
return ctx.badRequest(null, err);
}
}
}
现在,在客户端中,我使用 React 的 Promise 来调用我们的电子邮件扩展,如下所示:
await strapi.request('POST', '/email', {
data: {
to: confirmationEmailAddress,
subject: `Order Confirmation - South of Sleep ${new Date(Date.now())}`,
text: 'Your order has been processed',
html: '<bold>Except your stuff to be broken upon arriving</bold>'
}
});
有用!该电子邮件是从 Strapi 发送的,我看到控制器中的信息是电子邮件的一部分。问题?
客户端中的 Promise 是 try/catch 的一部分,在电子邮件被触发后,它会返回 404,因为它找不到/email
作为路线。所以,我不明白为什么它正在工作并找到电子邮件的扩展控制器,触发电子邮件但返回 404 路由。
我从客户端错误地调用扩展控制器。