Skip to content

Instantly share code, notes, and snippets.

@edharmowongso
Created September 12, 2020 10:03
Show Gist options
  • Save edharmowongso/99a1d3ce365f6c2cbafc47c5623129eb to your computer and use it in GitHub Desktop.
Save edharmowongso/99a1d3ce365f6c2cbafc47c5623129eb to your computer and use it in GitHub Desktop.
Controller File (campaign.js) - Update campaign.js to load model from MongoDB
// Location: controller/campaign.js
const {
models: {
mongo: { campaign: Campaign }
}
} = require('@pomona/pomona2-dbmodels')
const Joi = require('@hapi/joi')
Joi.objectId = require('joi-objectid')(Joi)
exports.GetCampaignsByBrandId = async (req, res, next) => {
try {
const validatedQuery = await Joi.validate(
req.query,
Joi.object().keys({
brand_id: Joi.objectId().required()
}),
{ stripUnknown: true }
)
const campaign = await Campaign.find({
brand_id: validatedQuery.brand_id
})
return res.json(campaign)
} catch (err) {
return next(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment