nazirov91 / ra-strapi-rest

React Admin data provider for Strapi.js
125 stars 40 forks source link

Missing ctx.query parameter in <Model>.count #6

Closed essamsalah closed 4 years ago

essamsalah commented 5 years ago

I noticed that using the code as it-is from the Example will throw an error in Strapi console

TypeError: Cannot read property 'source' of undefined

I fixed the error by sending the ctx.query as a parameter in the count method plus using the fully qualified class name as below: ctx.set('Content-Range', await strapi.services.<Model>.count(ctx.query));

My Node/Strapi : Version: 3.0.0-alpha.25.2 (node v11.1.0)

TitoVince35 commented 5 years ago

Here is another suggestion that would make it easyier to copy/paste in every controller:

module.exports = {
  find: async ctx => {
    const modelName = "<ModelName>";
    const srv = strapi.services[modelName];
    ctx.set("Content-Range", await srv.count(ctx.query));
    if (ctx.query._q) {
      return srv.search(ctx.query);
    } else {
      return srv.find(ctx.query);
    }
  }
};
nazirov91 commented 4 years ago

Thank you @essamsalah. Updated the readme file 👍