user14709104
user14709104

Reputation:

Create custom API with strapi

I've a content type called continent. Which we the name suggests contains all the information about each continents. Strapi already created API endpoints for me like

continents/:id

But I want to search the continent by it's name since the general user won't be able to search by id

I've created the endpoint

continents/:continent_name

I've also created custom controller following documentation

const { sanitizeEntity } = 
requiree('strapi-utils');

module.exports = {


async findOne(ctx) {
const { continent_name } = ctx.params;

const entity = await 
strapi.services.continent.findOne({ 
continent_name 
});
return sanitizeEntity(entity, { model: 
continents });

And also exposed the API to public But doesn't seem to anything Just returns error

How am I supposed to do it

Upvotes: 1

Views: 937

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 710

For your use case, you don't need to extend the model controller. You can just pass the continent name as a query param . For example, your url could be something like base_url/continent?continent_name=Asia.

For the code mentioned in the question, there is an issue, the model name should be strapi.models.continent and not continents. Also in the first line requiree('strapi-utils'), you have an extra e in the require. I am assuming that was just a typo.

Upvotes: 1

Related Questions