【问题标题】:Javascript: Nested JSON api request with multiple query parametersJavascript:具有多个查询参数的嵌套 JSON api 请求
【发布时间】:2016-05-24 14:33:08
【问题描述】:

我尝试为我的下一个网站创建一个 API,但我无法在一个快速应用程序中使用相同的 url 获得多个查询结果。

虚拟数据:

var data = [{
    articles : [{
        id : '0',
        url : 'foo',
        title : 'Foo',
        body : 'some foo bar',
        category : 'foo',
        tags : [
            'foo'
        ]
    }, {
        id : '1',
        url : 'foo-bar',
        title : 'Foo bar',
        body : 'more foo bar',
        category : 'foo',
        tags : [
            'foo', 'bar'
        ]
    }, {
        id : '2',
        url : 'foo-bar-baz',
        title : 'Foo bar baz',
        body : 'more foo bar baz',
        category : 'foo',
        tags : [
            'foo',
            'bar',
            'baz'
        ]
    }]
}, {
    users : [{
        name: 'Admin'
    }, {
        name: 'User'
    }]
}];

路由器:

// Grabs articles by categories and tags
// http://127.0.0.1:3000/api/articles/category/foo/tag/bar
router.get('/articles/category/:cat/tag/:tag', function(req, res) {
  var articles = data[0].articles;
  var q = articles.filter(function (article) {
    return article.category === req.params.cat;
    return article.tags.some(function(tagId) { return tagId === req.params.tag;});
  });
  res.json(q);
});

如果我请求http://127.0.0.1:3000/api/articles/category/foo/tag/bar url,如何嵌套结果?现在如果我这样做,tag url 将被忽略,只有category 请求有效。

感谢您的帮助!

【问题讨论】:

    标签: javascript json node.js api express


    【解决方案1】:

    您需要重写您的return 语句如下:

    return article.category === req.params.cat &&
           article.tags.some(function(tagId) {
               return tagId === req.params.tag;
           });
    

    ...使用&& operator,否则您将只测试第一个条件,永远不会到达另一个语句。

    以下是请求在类别“foo”和标签“bar”上时的测试:

    var data =
    [
      { articles:
        [
          { id: '0', url: 'audrey-hepburn', title: 'Audrey Hepburn', body: 'Nothing is impossible, the word itself says \'I\'m possible\'!', category: 'foo', tags: [ 'foo' ] },
          { id: '1', url: 'walt-disney', title: 'Walt Disney', body: 'You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you.', category: 'foo', tags: [ 'foo', 'bar' ] },
          { id: '2', url: 'unknown', title: 'Unknown', body: 'Even the greatest was once a beginner. Don\'t be afraid to take that first step.', category: 'bar', tags: [ 'foo', 'bar', 'baz' ] },
          { id: '3', url: 'neale-donald-walsch', title: 'Neale Donald Walsch', body: 'You are afraid to die, and you\'re afraid to live. What a way to exist.', category: 'bar', tags: [ 'foo', 'bar', 'baz' ] }
        ]
      },
      { users:
        [
          { name: 'Admin' },
          { name: 'User' }
        ]
      }
    ];
    
    req = {params: {cat: 'foo', tag: 'bar'}};
    
    var articles = data[0].articles;
    var q = articles.filter(function (article) {
        return article.category === req.params.cat &&
               article.tags.some(function(tagId) { 
                   return tagId === req.params.tag;
               });
    });
    
    document.write('<pre>', JSON.stringify(q, null, 4), '</pre>');

    看看它如何匹配最后一个条目。

    【讨论】:

    • 我想我可能误解了。你想做这样的请求:/articles/category/foo/tag/bar。那正确吗?你能不能放回原来的router.get(/articles/category/:cat/tag/:tag', ... URL 模式,在函数中只输出res.json(req.params);。我想确保你看到params 中的机器人类别和标签。
    • OK,这意味着 URL 模式工作正常。忘记我写的所有内容,我将答案恢复到以前的样子。现在让我检查一下还有什么问题......
    • 我使用您在 github 上的数据更新了我的答案,并将 || 运算符替换为 &amp;&amp;,这要求同时满足 cat 和 tag 条件。
    • 我稍微美化了api路由器。再次感谢您的帮助! :) github.com/DJviolin/horgalleryNode/blob/master/routes/api.js
    • 不,我的意思是当您将article.category === req.params.cat 移动到some 回调函数中时,实际上是乘以该测试的执行,这将始终相同。但实际上,这不应该对性能产生太大影响。没关系。
    【解决方案2】:

    问题来了>>

    return article.category === req.params.cat;
    return article.tags.some(function(tagId) { return tagId === req.params.tag;});
    

    第一个return 语句将停止函数内的进一步操作 所以你需要 if ... elsecase 或这里的辅助函数

    【讨论】:

    • return article.category === req.params.cat, article.tags.some(function(tagId) { return tagId === req.params.tag;}); 似乎工作。这是一个很好的解决方案吗?编辑:不。返回每个类别。代之以标签。
    • 不,return 在这种情况下将执行所有语句,但返回最后一条语句
    • 如果[articles].indexOf(el)[tags].indexOf(el)都是数组,你可以试试。
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2022-01-16
    • 2017-08-11
    • 2020-03-15
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多