我嘗試爲我的下一個網站創建一個API,並且我無法在快速應用中使用相同的URL獲取多個查詢結果。Javascript:帶有多個查詢參數的嵌套JSON API請求
虛擬數據:
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
網址的結果?現在如果我這樣做,tag
網址被忽略,只有category
請求有效。
謝謝你的幫助!
使用這也只是'category'請求工作和標記被忽略。我用'res.json(q);'請求。 – Lanti
這很奇怪。當我在我的應用程序中使用完全相同的代碼時,API請求的標記參數不起作用。這裏是我的回購網址:https://github.com/DJviolin/horgalleryNode/blob/master/routes/api.js – Lanti
我在我的代碼中放置了更新的URL,但由於某種原因,我在服務器啓動時出錯:'SyntaxError:無效的正則表達式:/^\/articles \ /(?(?:([^ \ /] +?))\ /(?:([^ \ /] +?))| tag \ /( ?:(([^ \ /] +?)))\ /?$ /:無效的組。 – Lanti