Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET request 'find by name' instead of find by id

Is there a way for me use this code or a similar code to find by username instead of findById?

Router.route('/:id').get(function (req, res)
 {
    let id = req.params.id;
    Books.findById(id, function (err, course) {
        res.json(course);
    });
});
like image 216
Chamika Prabath Avatar asked Nov 24 '25 13:11

Chamika Prabath


1 Answers

Books.find({username: 'username'},function (err, course) {res.json(course);}); 

or

Books.find({username: /username/i},function (err, course) {res.json(course);}); // Like operator

I hope it works Look at Mongoose Docs here

like image 154
Shubh Avatar answered Nov 26 '25 04:11

Shubh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!