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