Javascript:具有多个查询参数的嵌套 JSON api 请求

Javascript: Nested JSON api request with multiple query parameters

本文关键字:嵌套 JSON api 请求 参数 查询 Javascript      更新时间:2023-09-26

我尝试为我的下一个网站制作一个 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请求才有效。

谢谢你的帮助!

您需要

按如下方式重写return语句:

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

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

以下是当请求在类别"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>');

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

问题就在这里>>

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

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