Tuesday, 23 May 2017

mongoose route return specific field for given ID

I am trying to build an API which returns data from mongoDB based on the input. The strucutre of my db looks like this:

id    trips_2014    trips_2015
72    [...]         [...]
85    [...]         [...]

And my routes looks like this:

    app.get('/trips/:id', function(req,res) {

        Trip.findById(req.params.id, function (err, doc){
            if(err)
                res.send(err);
            res.json(doc);
        });
    });
}

So currently when called e.g. with trips/72, it returns both trips_2014 and trips_2015. How can I create a query from this dynamically such that I can have another input determining which year it should return?

I thought of something like this: /trips/:id/:yyyy, from which I guess I can use req.params.yyyy somehow in the query?



via ffritz

No comments:

Post a Comment