基于JSON的Express Api的Get请求中存在多个查询值
我遵循了这个教程。然而,即使在复制源代码时,我似乎也无法让它工作。我不确定我是否做了错事,我错过了。基本上,当使用2个查询参数时,我会得到响应中返回的所有记录。它只适用于一种罚款。api路由是基于JSON的Express Api的Get请求中存在多个查询值,json,node.js,rest,express,lodash,Json,Node.js,Rest,Express,Lodash,我遵循了这个教程。然而,即使在复制源代码时,我似乎也无法让它工作。我不确定我是否做了错事,我错过了。基本上,当使用2个查询参数时,我会得到响应中返回的所有记录。它只适用于一种罚款。api路由是 app.get('/api/stores', function(req, res){ var response = []; console.log(req.query) // this would usually adjust your database query if(typeof
app.get('/api/stores', function(req, res){
var response = [];
console.log(req.query)
// this would usually adjust your database query
if(typeof req.query.nsfw != 'undefined'){
stores.filter(function(store){
if(store.nsfw.toString() == req.query.nsfw){
response.push(store);
}
});
}
// this would usually adjust your database query
if(typeof req.query.location != 'undefined'){
stores.filter(function(store){
if(store.location === req.query.location){
response.push(store);
}
});
}
// de-duplication:
response = _.uniqBy(response, 'id');
// in case no filtering has been applied, respond with all stores
if(Object.keys(req.query).length === 0){
response = stores;
}
res.json(response);
});
JSON文件是
const stores = [
{
id: 1,
name: 'Hollows End Grim Potions',
imageURL: 'https://images.unsplash.com/photo-1465433360938-e02f97448763? auto=format&fit=crop&w=1267&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
location: 'Hollows End',
nsfw: true
},
{
id: 2,
name: 'Lembas Food Truck',
imageURL: 'https://images.unsplash.com/reserve/DHHQbqc0RrWVf0uDNe5E_The%20Litte%20Cafe.jpg?auto=format&fit=crop&w=1489&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
location: 'Lothlorien',
nsfw: false
},
{
id: 3,
name: 'Galadriels Mirror of Brutal Truth',
imageURL: 'https://images.unsplash.com/photo-1497519098947-a305f214d3bc?auto=format&fit=crop&w=1350&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
location: 'Lothlorien',
nsfw: true
},
{
id: 4,
name: 'Jabbas Light Sabers',
imageURL: 'https://images.unsplash.com/photo-1481241857164-e8483bce922d?auto=format&fit=crop&w=1353&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
location: 'Mos Eisley',
nsfw: true
}
];
module.exports = stores;
所以我要重申我的问题——为什么如果我放了一个位置,我不仅会得到id:3?现在我得到了所有的记录
编辑:
我刚刚用它尝试了一下,奇怪的是我得到了两条记录ID:2和ID:3,而我应该只得到ID:2。你在分别筛选条件,并将每个条件的结果推到你的
响应数组中,而不是使用&
一起筛选这两个条件
例如,当在location=Lothlorien
或nsfw=true
上进行过滤时,该位置得到2个结果,而nsfw得到3个结果,其中唯一包括4个(全部)。然而,如果它必须同时满足这两个条件,您将期望得到一个结果——只有一个名为LotCllorien的位置的nsfw为false
下面是一个使用&&
的示例:
const { location=null, nsfw=null } = req.query;
if (location && nsfw) {
stores.filter((store) => {
if(store.nsfw.toString() === nsfw && store.location === location){
response.push(store);
}
});
}
您将分别筛选条件,并将每个条件的结果推送到响应
数组中,而不是使用&&
一起筛选这两个条件
例如,当在location=Lothlorien
或nsfw=true
上进行过滤时,该位置得到2个结果,而nsfw得到3个结果,其中唯一包括4个(全部)。然而,如果它必须同时满足这两个条件,您将期望得到一个结果——只有一个名为LotCllorien的位置的nsfw为false
下面是一个使用&&
的示例:
const { location=null, nsfw=null } = req.query;
if (location && nsfw) {
stores.filter((store) => {
if(store.nsfw.toString() === nsfw && store.location === location){
response.push(store);
}
});
}
我是通过这篇文章评论中的链接来到这里的。我也希望服务器代码的行为有所不同,所以我对它进行了一些修改,得到了我想要的结果。我希望这能回答最初海报的问题。这是我的整个server.js文件:
const express = require('express');
const app = express();
const stores = require('./data/stores');
// npm i express-query-boolean
const boolParser = require('express-query-boolean');
app.use(boolParser());
app.get('/', function (req, res) {
res.send('Hello World!')
});
app.get('/api/stores', function (req, res) {
let response = [];
const q = req.query; // Query object
if (Object.keys(q).length === 0) {
// NO query parameters, send it all...
response = stores;
} else {
// We have a query, filter response to match request
response = stores.filter(function (store) {
return Object.keys(this).every((key) => store[key] === this[key]);
}, q);
}
res.json(response);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
我是通过这篇文章评论中的链接来到这里的。我也希望服务器代码的行为有所不同,所以我对它进行了一些修改,得到了我想要的结果。我希望这能回答最初海报的问题。这是我的整个server.js文件:
const express = require('express');
const app = express();
const stores = require('./data/stores');
// npm i express-query-boolean
const boolParser = require('express-query-boolean');
app.use(boolParser());
app.get('/', function (req, res) {
res.send('Hello World!')
});
app.get('/api/stores', function (req, res) {
let response = [];
const q = req.query; // Query object
if (Object.keys(q).length === 0) {
// NO query parameters, send it all...
response = stores;
} else {
// We have a query, filter response to match request
response = stores.filter(function (store) {
return Object.keys(this).every((key) => store[key] === this[key]);
}, q);
}
res.json(response);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
很好,这很有道理。虽然我确实把教程抄到了信上,所以我想那家伙需要更新一下。我现在需要去弄清楚在这种情况下如何使用&&了。ThxI添加了一个使用&&
的示例。如果有帮助或者您有问题,请告诉我。谢谢您,先生,这正是我所需要的@PaulLeppard教程很好(我也读过),作者想要的是显示以Lotlorien作为位置或nsfw作为true的值。如果你注意到不会有一个nsfw false和另一个位置不是LOTROLIEN的响应。太好了,这是有道理的。虽然我确实把教程抄到了信上,所以我想那家伙需要更新一下。我现在需要去弄清楚在这种情况下如何使用&&了。ThxI添加了一个使用&&
的示例。如果有帮助或者您有问题,请告诉我。谢谢您,先生,这正是我所需要的@PaulLeppard教程很好(我也读过),作者想要的是显示以Lotlorien作为位置或nsfw作为true的值。如果您注意到将不会有nsfw false和另一个非LOTLOLIEN位置的响应。