te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Strapi v5 overriding controller action deletes other default actions - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Strapi v5 overriding controller action deletes other default actions - Stack Overflow

programmeradmin3浏览0评论

I've got a weird issue here, I overrode the find() action inside of a custom controller in strapi v5 like this:

'use strict';

/**
 * team controller
 */

const {createCoreController} = require('@strapi/strapi').factories;

module.exports = createCoreController('api::team.team', ({strapi}) => ({
  async create(ctx) {
    try {
      const user = ctx.state.user;

      if (!user) {
        return ctx.unauthorized('You must be logged in to create a team');
      }

      const teamData = {
        ...ctx.request.body.data,
        user: user.id
      }

      const team = await strapi.entityService.create('api::team.team', {
        data: teamData,
      });

      return ctx.created(team);
    } catch (error) {
      throw ctx.throw(500, error);
    }
  },

  async findOne(ctx) {
    const {data, meta} = await super.findOne(ctx);
    return {data, meta};
  }
}));

Unfortunately this seems to remove all other controller actions like findOne() from the controller entirely. How can I fix this?

Thanks in advance for any useful input.

I've got a weird issue here, I overrode the find() action inside of a custom controller in strapi v5 like this:

'use strict';

/**
 * team controller
 */

const {createCoreController} = require('@strapi/strapi').factories;

module.exports = createCoreController('api::team.team', ({strapi}) => ({
  async create(ctx) {
    try {
      const user = ctx.state.user;

      if (!user) {
        return ctx.unauthorized('You must be logged in to create a team');
      }

      const teamData = {
        ...ctx.request.body.data,
        user: user.id
      }

      const team = await strapi.entityService.create('api::team.team', {
        data: teamData,
      });

      return ctx.created(team);
    } catch (error) {
      throw ctx.throw(500, error);
    }
  },

  async findOne(ctx) {
    const {data, meta} = await super.findOne(ctx);
    return {data, meta};
  }
}));

Unfortunately this seems to remove all other controller actions like findOne() from the controller entirely. How can I fix this?

Thanks in advance for any useful input.

Share Improve this question asked 2 days ago derelektrischemoenchderelektrischemoench 4031 gold badge4 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Well... after some researching I figured it out. Basically I required the controller and bound routes for all of the instance methods (i.e. create(), update() etc.). The controller looks like this:

const {createCoreController} = require('@strapi/strapi').factories;

module.exports = createCoreController('api::team.team', ({strapi}) => ({
  async create(ctx) {
    try {
      const user = ctx.state.user;

      if (!user) {
        return ctx.unauthorized('You must be logged in to create a team');
      }

      const teamData = {
        ...ctx.request.body.data,
        user: user.id
      }

      const team = await strapi.entityService.create('api::team.team', {
        data: teamData,
      });

      return ctx.created(team);
    } catch (error) {
      throw ctx.throw(500, error);
    }
  }
}));

When this was done I bound the create round in my routes.js file:

module.exports = {
  routes: [
    {
      method: 'POST',
      path: '/create-team/submit',
      handler: 'team.create',
      config: {
        policies: [],
        middlewares: [],
      },
    },
}

Unfortunately overriding just one route deletes all other bound methods (i.e. find(), findOne() etc. This means that they all have to be set manually like this in routes/apiName.js

module.exports = {
  routes: [
    {
      method: 'POST',
      path: '/create-team/submit',
      handler: 'team.create',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'GET',
      path: '/teams',
      handler: 'team.find',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'GET',
      path: '/teams/:id',
      handler: 'team.findOne',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'PUT',
      path: '/teams/:id',
      handler: 'team.update',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'DELETE',
      path: '/teams/:id',
      handler: 'team.delete',
      config: {
        policies: [],
        middlewares: [],
      },
    },
  ],
};

And that was basically it. All the endpoints for CRUD operations work fine and the custom controller is called for the create() method.

Having to manually set all the bound routes seems kind of fishy to me, but at least it works. If someone finds a more elegant solution to this, I'd be very interested to see what that would look like.

发布评论

评论列表(0)

  1. 暂无评论