Reputation: 40084
I have a store and some products can be unpublished - when viewed on front they are 404. However I would like to preview them if "preview=1" is given in the url. I am looking for an elegant, one liner way to insert that in my find function:
var preview = req.param('preview');
Product.findOne({ _id: req.params.id, publish: TRUE_OR_ANY_IF_PREVIEW_SET)}).populate('categories').exec(function(err, product) { }
Upvotes: 2
Views: 721
Reputation: 146034
Product.findOne({
_id: req.param('id')
publish: req.param('preview') ? true: false
}).populate('categories').exec(function(err, product) { });
Upvotes: 1
Reputation: 311865
It's not quite a one-liner, but here's the typical way to do it:
var query = {_id: req.params.id};
if (!preview) {
query.publish: true
}
Product.findOne(query).populate('categories').exec(function(err, product) { }
Upvotes: 4