.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 (.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
}
}
}
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
}
}
}
Share
Improve this question
edited Oct 28, 2020 at 0:26
Dora
asked Oct 27, 2020 at 19:36
DoraDora
7,01015 gold badges58 silver badges116 bronze badges
3
- Can you share your model code? – Arun Kumar Mohan Commented Oct 27, 2020 at 19:41
- @ArunKumarMohan added – Dora Commented Oct 28, 2020 at 0:26
- The native Draft & Publish feature has been released in version 3.2. We suggest you to use the native feature instead of this guide. This guide is still useful if you want to see the concept of "force filtering" in action. – gandharv garg Commented Jan 11, 2021 at 11:50
3 Answers
Reset to default 1As 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.
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": []
}
}
]
}