2016-02-13 91 views
0

我嘗試爲我的下一個網站創建一個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請求有效。

謝謝你的幫助!

回答

1

你需要重寫你的return聲明如下:

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

...使用&& operator,否則你只會在第一個條件下測試,從未達到其他語句。

下面是測試時的要求是在類別「富」和標記「巴」:

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>');

看看它是如何在最後一項比賽。

+0

使用這也只是'category'請求工作和標記被忽略。我用'res.json(q);'請求。 – Lanti

+0

這很奇怪。當我在我的應用程序中使用完全相同的代碼時,API請求的標記參數不起作用。這裏是我的回購網址:https://github.com/DJviolin/horgalleryNode/blob/master/routes/api.js – Lanti

+0

我在我的代碼中放置了更新的URL,但由於某種原因,我在服務器啓動時出錯:'SyntaxError:無效的正則表達式:/^\/articles \ /(?(?:([^ \ /] +?))\ /(?:([^ \ /] +?))| tag \ /( ?:(([^ \ /] +?)))\ /?$ /:無效的組。 – Lanti

1

問題是在這裏>>

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

第一return語句將功能 所以你需要停止內採取進一步行動if ... elsecase或這裏輔助函數

+0

'return article.category === req.params.cat,art​​icle.tags.some(function(tagId){return tagId === req.params.tag;});'看起來像是工作。這是一個好的解決方案嗎?編輯:不。返回每個類別。標籤正在工作。 – Lanti

+1

不,在這種情況下,'return'將執行所有操作,只是返回最後一條語句 – loadaverage

+0

如果兩者都是數組,您可以嘗試'[articles] .indexOf(el)'和'[tags] .indexOf(el)'。 – loadaverage