5

https://strapi.io/documentation/v3.x/guides/draft.html#apply-our-changes

I am customizing my own API, which of course I would like to filter only posts with status published, so I followed the documentation above to see how it works.

I actually use the exact code except for my own model so my code is below

'use strict';
const { sanitizeEntity } = require('strapi-utils');

/**
 * Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
 * to customize this controller
 */

module.exports = {
  async find(ctx) {
    let entities;

    console.log(ctx.query, 'before');
    ctx.query = {
      ...ctx.query,
      status: 'published',
    };

    console.log(ctx.query, 'after');
    if (ctx.query._q) {
      entities = await strapi.services.post.search(ctx.query);
    } else {
      entities = await strapi.services.post.find(ctx.query);
    }

    return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.post }));
  }
};

then I do a normal get API call localhost:1337/posts

but I get this error though

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "Your filters contain a field 'status' that doesn't appear on your model definition nor it's relations"
}

I will be adding things on later for the query as needed but even following the documentation, this error occurs, if I am not overriding the default controller, the API works fine.

Thank you in advance for any suggestions and advice.

EDIT:

models/post.settings.json

{
   "kind":"collectionType",
   "collectionName":"posts",
   "info":{
      "name":"Post",
      "description":""
   },
   "options":{
      "increments":true,
      "timestamps":true,
      "draftAndPublish":true
   },
   "attributes":{
      "title":{
         "type":"string",
         "required":true
      },
      "images":{
         "collection":"file",
         "via":"related",
         "allowedTypes":[
            "images"
         ],
         "plugin":"upload",
         "required":false
      },
      "publishedAt":{
         "type":"datetime"
      },
      "expiresAt":{
         "type":"datetime"
      },
      "content":{
         "type":"richtext"
      },
      "link":{
         "type":"string"
      },
      "categories":{
         "collection":"category",
         "via":"posts"
      },
      "slug":{
         "type":"uid",
         "targetField":"title"
      },
      "originalPrice":{
         "type":"decimal"
      },
      "salePrice":{
         "type":"decimal"
      },
      "thumb":{
         "model":"file",
         "via":"related",
         "allowedTypes":[
            "images"
         ],
         "plugin":"upload",
         "required":false
      }
   }
}
Dora
  • 6,776
  • 14
  • 51
  • 99

3 Answers3

1

As the error message says, you don't have a status field in your model. Add an enumeration field status with the values draft, published, and archived to your model as mentioned here.

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • 1
    ok but by doing so, that means we cannot use the default `status` by strapi, which means the publish / unpublish button from admin dashboard would be useless. Instead we will use the `enumeration` field created instead? – Dora Oct 28 '20 at 03:19
0

enter image description here

The order of the routes matters!!!

Renderlife
  • 50
  • 4
  • Could you find a way to augment your answering picture of free-hand adorned text with a text-only version? That would make your answer helpful for more people. – Yunnosch Nov 19 '21 at 06:59
0

As @Renderlife has mentioned, order of routes matters. For example

Below code will not work, it will throw error, if we try to access http://localhost:1337/bookings/feedback? , however if we change order of json object then it will work as expected.


{
  "routes": [
    {
      "method": "GET",
      "path": "/bookings",
      "handler": "booking.find",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/count",
      "handler": "booking.count",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/:id",
      "handler": "booking.findOne",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/feedback",
      "handler": "booking.feedback",
      "config": {
        "policies": []
      }
    },
    {
      "method": "POST",
      "path": "/bookings",
      "handler": "booking.create",
      "config": {
        "policies": []
      }
    },
    {
      "method": "PUT",
      "path": "/bookings/:id",
      "handler": "booking.update",
      "config": {
        "policies": []
      }
    },
    {
      "method": "DELETE",
      "path": "/bookings/:id",
      "handler": "booking.delete",
      "config": {
        "policies": []
      }
    }
  ]
}


Working example: look at the order of /bookings/feedback


{
  "routes": [
    {
      "method": "GET",
      "path": "/bookings",
      "handler": "booking.find",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/count",
      "handler": "booking.count",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/feedback",
      "handler": "booking.feedback",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/bookings/:id",
      "handler": "booking.findOne",
      "config": {
        "policies": []
      }
    },

    {
      "method": "POST",
      "path": "/bookings",
      "handler": "booking.create",
      "config": {
        "policies": []
      }
    },
    {
      "method": "PUT",
      "path": "/bookings/:id",
      "handler": "booking.update",
      "config": {
        "policies": []
      }
    },
    {
      "method": "DELETE",
      "path": "/bookings/:id",
      "handler": "booking.delete",
      "config": {
        "policies": []
      }
    }
  ]
}


Anjum....
  • 4,086
  • 1
  • 35
  • 45