![](/img/trans.png)
[英]Strapi Beta with custom Sendgrid Controller code for email
[英]Custom Controller code for Strapi Beta (3.0)
我有一些來自以前版本的 Strapi 的代碼可以工作,而控制器的 beta 版本則大不相同。 添加了多部分/消毒樣板,並且發生了一些變化。 不明白如何整合我的訂單 object 和條帶充電。
這是添加的樣板:
module.exports = {
async create(ctx) {
// New Boilerplate added with Strapi Beta - how to integrate this with custom stuff below?
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
entity = await service.create(data, { files });
} else {
entity = await service.create(ctx.request.body);
}
return sanitizeEntity(entity, { model });
}
}
這是我的自定義代碼(controller 名稱是 Order.js)
const { address, amount, products, postalCode, token, city } = ctx.request.body;
// Send charge to Stripe
const charge = await stripe.charges.create({
amount: amount * 100,
currency: 'usd',
description: `Order ${new Date(Date.now())} - User ${ctx.state.user.id}`,
source: token
});
// Create order in database
const order = await strapi.services.order.add({
user: ctx.state.user.id,
address,
amount,
products,
postalCode,
city
});
看起來我會將我的代碼添加到 if 語句的第二部分,因為它不是多部分的,但如果“實體”是 Strapi 需要的真實變量名或我重命名為“訂單”的占位符變量,則不是用戶代碼在 Alpha 中可以正常工作,但閱讀 Strapi 文檔,並沒有解釋如何將此結構與“實體”、{模型}和“數據”變量一起使用。
在以前版本的 Strapi 中,要將文件上傳到新條目,您必須首先創建條目,然后再上傳圖像並指定要鏈接此圖像的條目。 現在有了多部分,您可以在發送您的圖像的同時發送您的條目屬性。
現在關於您的用例, service.
在你的情況下,必須用strapi.api.order.service.order
替換。 我同意文檔不清楚。 我會立即更新。
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
module.exports = {
async create(ctx) {
// New Boilerplate added with Strapi Beta - how to integrate this with custom stuff below?
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
entity = await strapi.api.order.services.order.create(data, { files });
} else {
const { address, amount, products, postalCode, token, city } = ctx.request.body;
// Send charge to Stripe
const charge = await stripe.charges.create({
amount: amount * 100,
currency: 'usd',
description: `Order ${new Date(Date.now())} - User ${ctx.state.user.id}`,
source: token
});
entity = await strapi.api.order.services.order.create({
user: ctx.state.user,
address,
amount,
products,
postalCode,
city
});
}
return sanitizeEntity(entity, { model: strapi.query('order').model });
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.