gpt4 book ai didi

javascript - 如何查询包含特定键/值对的文档

转载 作者:行者123 更新时间:2023-11-28 04:49:20 25 4
gpt4 key购买 nike

使用mongo的shell, db.collection.find( {key:value} )返回正是我想要的,只是我想使用像 localhost:8000/api/profiles?key=value

这样的 URL 从浏览器进行此查询

我正在尝试使用 Express、Node 和 Mongoose。

这是我的代码:

//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);
};

您看不到的是“Iprofile”需要我的 Mongoose 模式。我不知道我做错了什么。

最佳答案

由于您使用的是查询参数 ( difference between req.query and req.params ),因此无需定义 /:param。您需要使用req.query。

此外,您没有将 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);
};

关于javascript - 如何查询包含特定键/值对的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43077598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com