[英]How do I query for documents that contain a specific key/value pair
Using monogo's shell, db.collection.find( {key:value} )
returns exactly what I want, only I want to make this query from a browser using a URL like localhost:8000/api/profiles?key=value
使用monogo的shell,
db.collection.find( {key:value} )
返回我想要的内容,只是我想使用像localhost:8000/api/profiles?key=value
这样的URL从浏览器进行此查询localhost:8000/api/profiles?key=value
I'm trying to use Express, Node and Mongoose. 我正在尝试使用Express,Node和Mongoose。
Here is the code I have: 这是我的代码:
//Get By query route
router.get('/:param', function(req, res){
var param = req.query(param);
getProfilebyQuery(function(err, profiles) {
if(err){
throw err;
}
res.json(profiles)
})
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
Iprofile.find(param, callback);
};
What you can't see is "Iprofile" requires my mongoose schemas. 你看不到的是“Iprofile”需要我的猫鼬模式。 I can't tell what I'm doing wrong.
我不知道我做错了什么。
Since you're using query parameters ( difference between req.query and req.params ), you don't need to define /:param
. 由于您使用的是查询参数( req.query和req.params之间的差异 ),因此您无需定义
/:param
。 You need to use req.query. 您需要使用req.query。
Also you are not passing the param
variable to function. 另外,您没有将
param
变量传递给函数。
//Get By query route
router.get('/', function(req, res){
var param = req.query;
getProfilebyQuery(param, function(err, profiles) { //pass param
if(err){
throw err;
}
res.json(profiles)
})
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
Iprofile.find(param, callback);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.