javascript - Mongoose .find with a req.params array? and res.json an array of each value and count? -
ideally user able enter multiple tags in search field, delinienated client-side (before .get call), sent on ajax each keypress.
working on server-side (testing api postman), if .get serves variable tags array of [ 'tag1', 'tag2'], can req.params.tags array?
if so, server - right now, getting response postman localhost:4200/api/v1/tag/tagname .. not /tag/tagname1+tagname2 or /tag/tagname1&tagname2
here's current api:
router.route('/tag/:tags') // list of tags usergenerated tags (accessed @ http://localhost:4200/api/v1/tag/:tags) .get(function(req, res) { story.count( { tags: { $in: [ req.params.tags ] } }, function (err, count) { if (err) res.send(err); console.log('there is/are %d story/ies this/these tag(s)', count); if (count >= 1) { story.find( { tags: { $in: [ req.params.tags ] } }, 'tags', function(err, stories) { if (err) res.send(err); res.json(stories); // return count of each tag tag }); // find , return tags } // if count >= 1 if (count < 1) { res.json({ message: count + ' results' }); } // if count < 1 }); // .count }); // list of tags
secondly, need use .aggregate (have yet explore really) res.json array of tags can loop on each tagname , count each specific tag.
this isn't mongo-specific question, it's express question (i'm assuming using express here).
there isn't way express automatically parse dynamic segment (the part prefixed :
in url) array, can yourself:
// example url: /tags/tag1,tag2 app.get('/tags/:tags', function(req, res){ var tags = req.params.tags.split(','); // ['tag1','tag2'] res.send('you entered tags :' + tags.join(',')); });
here's runnable example: http://runnable.com/u5si2eafzmh7sfkt/example-reading-an-array-of-arguments-in-req-params-with-express-for-node-js-and-hello-world
Comments
Post a Comment