简体   繁体   中英

Passing REST URL parameter as query condition in Mongoose

Newbie in nodejs/mongoDB here. 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. 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

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 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.

I understand that this may be very basic question but I get result when I use findById for _id field. But client will call only with the specific field.

Appreciate your help.

Not Sure if i can call it stackoverflow luck. I overlooked the quotes in get method in the model. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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