javascript - How to search an array built with a select multiple in Mongoose/Express -
i building search form relies on select multiple attribute.
here code form.
<form action="/search" method="get"> <select name="q" multiple> <optgroup label="fruit"> <option value="peach">peach</option> <option value="apple">apple</option> <option value="pear">pear</option> <optgroup label="meat"> <option value="chicken">chicken</option> <option value="beef">beef</option> <option value="pork">port</option> <optgroup label="dairy"> <option value="milk">milk</option> <option value="cheese">cheese</option> <option value="yogurt">yogurt</option> </select> </form> here part of mongoose schema.
// db.js var recipeschema = new schema({ user_id : string, name : string, ingredients : [ string ], updated_at : date }); here search route callback.
// routes/index.js
exports.search = function(req, res) { recipe.find({ 'ingredients' : { $all: [req.query.q] }, 'name', function(err, recipes) { if (err) return handleerror(err); res.render('./index', { title: "recipe book", recipes : recipes}); }); };
if pass 1 value http://yourserver:3000/?q=milk, of recipes have milk ingredient displayed; however, wtih 2 valueshttp://yourserver:3000/search?q=milk&q=beef nothing displayed.
view displaying results
.row p welcome #{title} table.table thead th name tbody each recipe in recipes tr td= recipe.name i can't see doing wrong or why isn't working. ideas appreciated
completely revising answer correct:
console.log(req.param('q')); calling .param() function compile parameters array.
Comments
Post a Comment