System Information
- Strapi Version: 3.6.2
- Operating System: MacOs
- Database: MongoDB
- Node Version: 14.17.0
- NPM Version: 6.14.13
- Yarn Version: 1.22.10
I’m new to both Strapi and Mongoose, so I apologise if this is a stupid question.
Following the docs I’m trying to create a custom query in Strapi in which I want to return the whole collection called people
sorted by name desc. But when I hit the endpoint I get a 500 error and checking the terminal the error message is: CastError: Cast to ObjectId failed for value “alldesc” at path “_id” for model “people”.
here’s my code so far:
services/people.js
module.exports = {
findByNameDesc() {
const result = strapi
.query("people")
.model.find()
.sort({ name: "descending" });
return result.map((entry) => entry.toObject());
},
};
controllers/people.js
module.exports = {
async alldesc(ctx) {
const entities = await strapi.services.people.findByNameDesc(ctx);
return entities.map((entity) =>
sanitizeEntity(entity, { model: strapi.models.people })
);
},
};
config/routes.json
{
"routes": [
...
{
"method": "GET",
"path": "/people/alldesc",
"handler": "people.alldesc",
"config": {
"policies": []
}
}
]
}
What am I doing wrong? Even if I remove the sort() from the query I still get the same error.
Thank you in advance