Strapi Beta(3.0)的自定义控制器代码 [英] Custom Controller code for Strapi Beta (3.0)

查看:293
本文介绍了Strapi Beta(3.0)的自定义控制器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Strapi的先前版本中获得了一些有效的代码,而beta版的控制器则大不相同.添加了多部分/结束符样板,并且发生了一些变化.不了解如何整合我的订单对象和条纹费用.

I have some code from the previous version of Strapi that works, and the beta version of controllers is much different. There is multipart / santization boilerplate added and something has changed. Do not understand how to integrate my order object and stripe charge.

这是添加的样板:

  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 });
 }
  }

这是我的自定义代码(控制器名称为Order.js)

Here is my custom code (The controller name is 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语句的第二部分,因为它不是多部分的,但是如果"entity"是Strapi需要的真实变量名或我将其重命名为"order"的占位符变量,则不是用户在Alpha中效果很好,但请阅读Strapi文档,并且没有解释如何将这种结构与实体",{模型}和数据"变量一起使用.

It looks like I would add my code to the second part of the if statement since it's not multipart, but not user if "entity" is a real variable name Strapi needs or a placeholder variable I rename to "order" Code works fine in Alpha, but read the Strapi docs and there is no explanation to how to use this structure with "entity", {model} and "data" variables.

推荐答案

在Strapi的先前版本中,要将文件上传到新条目,您必须首先创建条目,然后再创建两个条目,然后上传图像并指定条目您想要链接此图像. 现在有了多部分功能,您可以与输入属性同时发送图像.

In the previous version of Strapi, to upload a file to a new entry you had to first, create your entry and two, upload the image and specify the entry you want to link this image. Now with the multipart, you can send your image at the same time as your entry attributes.

现在关于您的用例,在您的情况下,必须将service.替换为strapi.api.order.service.order. 我同意该文档尚不清楚!我将立即更新.

Now about your use-case, service. has to be replaced by strapi.api.order.service.order in your case. I agree the doc is not clear! I will update that right now.

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 });
  }
}

这篇关于Strapi Beta(3.0)的自定义控制器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆