Skip to content

Instantly share code, notes, and snippets.

@Saad-ISAA
Last active May 15, 2022 05:32
Show Gist options
  • Save Saad-ISAA/60b27b22f6994ed7dc45663d2a6b2228 to your computer and use it in GitHub Desktop.
Save Saad-ISAA/60b27b22f6994ed7dc45663d2a6b2228 to your computer and use it in GitHub Desktop.
A Strapi controller to fetch data from MongoDB
'use strict';
const { createCoreController } = require('@strapi/strapi').factories;
const { find, findOne, insertOne } = require('../../../helper/mongo');
module.exports = createCoreController('api::reports.reports', ({ strapi }) => ({
async create(ctx) {
const data = ctx.request.body.data;
// create reports collection
const entity = await insertOne(strapi.mongodb, 'reports', data);
return entity;
},
async find(ctx) {
// search from reports collection from report_param passed as filters
const entities = await find(strapi.mongodb, 'reports', { report_param: ctx.query.filters.report_param });
return entities;
},
async findOne(ctx) {
// fineOne reports collection based on id as URL param
const entities = await findOne(strapi.mongodb, 'reports', { id: ctx.params.id });
return entities;
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment