简体   繁体   English

在猫鼬中传递REST URL参数作为查询条件

[英]Passing REST URL parameter as query condition in Mongoose

Newbie in nodejs/mongoDB here. 这里的nodejs / mongoDB中的新手。 I've tried, with no success, to find the answer to my problem before posting here. 在尝试发布此问题之前,我一直没有找到成功的答案。

I'm creating a simple node RESTAPI get services with Mongoose. 我正在用Mongoose创建一个简单的节点RESTAPI get服务。 I'm trying to pass the field value of the collection to retrieve a specific document. 我正在尝试传递集合的字段值来检索特定文档。

Like this http://localhost:3000/infrakpi/ fieldvalue in the browser 像这样浏览器中的http:// localhost:3000 / infrakpi / fieldvalue

I've written this following piece of code. 我已经编写了以下这段代码。

app.get('/infrakpi/:system',(req, res) => {
    Infrakpi.getInfrakpiBySystem(req.params.system, function(err, infrakpibysystem){
        if (err) {
            throw err;
        }
        res.json(infrakpibysystem);

    }); 
});

I have defined the get method in my mongoose model like below. 我已经在猫鼬模型中定义了get方法,如下所示。

//Get Infrakpi by System
module.exports.getInfrakpiBySystem = function (system, callback) {
    Infrakpi.find({system: 'system'}, callback)
}

when system is passed as fieldvalue in the restURL, I want to retrieve the specific document in the collection. 当系统作为restURL中的fieldvalue传递时,我想检索集合中的特定文档。

I understand that this may be very basic question but I get result when I use findById for _id field. 我知道这可能是一个非常基本的问题,但是当我对_id字段使用findById时会得到结果。 But client will call only with the specific field. 但是客户只会致电特定字段。

Appreciate your help. 感谢您的帮助。

Not Sure if i can call it stackoverflow luck. 不知道我是否可以称它为stackoverflow运气。 I overlooked the quotes in get method in the model. 我忽略了模型中get方法中的引号。 Once I removed them, it worked. 一旦删除它们,它就起作用了。

//Get Infrakpi by System
module.exports.getInfrakpiBySystem = function (system, callback) {
    Infrakpi.find({system: system}, callback)
}

I'm leaving the question here without deleting, if it can help someone else in the future. 我将问题留在这里而不删除,如果将来可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM