"Your filters contain a field 'status' that doesn't appear on your model definition nor it's relations" (Strapi)

Viewed 5969

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
      }
   }
}
3 Answers

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.

enter image description here

The order of the routes matters!!!

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": []
      }
    }
  ]
}


Related