Last active
May 15, 2022 05:32
-
-
Save Saad-ISAA/60b27b22f6994ed7dc45663d2a6b2228 to your computer and use it in GitHub Desktop.
A Strapi controller to fetch data from MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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