猫鼬.找到一个请求.参数数组?res.json是每个值和计数的数组
Mongoose .find with a req.params array? and res.json an array of each value and count?
理想情况下,用户将能够在搜索字段中输入多个标记,然后在客户端(在.get调用之前)进行分隔,以便在每次按键时通过ajax发送。
在服务器端工作(使用Postman测试API),如果.get将变量标记作为['tag1', 'tag2']的数组提供,可以req.params.tags是数组吗?
如果是这样,它会是什么样子的服务器-现在,我只得到一个响应从邮差localhost:4200/api/v1/tag/tagName ..但/tag/tagName1+tagName2或/tag/tagnam1 &tagName2
下面是当前的API:
router.route('/tag/:tags')
// get the list of tags for userGenerated tags (accessed at GET 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 with 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); // something to return the count of each tag with the tag
}); // find and return tags
} // if count >= 1
if (count < 1) {
res.json({ message: count + ' results' });
} // if count < 1
}); // .count
}); // get list of tags
其次,我是否需要使用.aggregate(还没有真正探索这一点)然后为.json提供一个标记数组,我可以循环以获得每个tagName和每个特定标记的计数。
这实际上不是一个特定于蒙古人的问题,它是一个表达问题(我假设你在这里使用express)。
express无法自动将动态段(url中前缀为:
的部分)解析为数组,但您可以轻松地自己完成:
// 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(','));
});
下面是一个可运行的示例:http://runnable.com/U5SI2eAfzMh7Sfkt/example-reading-an-array-of-arguments-in-req-params-with-express-for-node-js-and-hello-world
相关文章:
- 为DataTables aoColumnDefs创建JavaScript数组(JSON格式)
- 使用 Javascript 将行添加到数组 json
- PHP数组JSON编码和该对象在ExtJs中解码
- 数组json转换为数组json
- 将Blob文本转换为Javascript中的数组/JSON对象
- 如何合并这些数组/json 对象
- 尝试从数组 JSON 键中删除引号
- JavaScript 中的数组 JSON
- 在数组 JSON 格式的 JavaScript 变量末尾添加新地址
- 如何访问这个Javascript数组(JSON对象?)
- rails传递记录数组(JSON)
- 按值提取对象/数组json vs数组
- 获取具有子数组json值的数组的值
- Object对象数组Json.字符串数组的字符串化
- 如何在Ext js 4.1中过滤数组json
- PHP -多维数组Json
- 正在分析数组Json以选择Tag
- 试图将字符串数据转换为数值数据,将数据放入数组的数组(Json)
- 从数组json、javascript中的specf值中删除引号
- 使用JavaScript或jQuery读取数组JSON