Search code examples
node.jsjsonapigetget-request

How to get request by type?


I'm kinda stuck on this one. I have a JSON file that is like this:

[
    {
     id: 123
     content: Hello my name is abc
     type: commentary
    }
]

I do know that you can call an api using an ID as such. Example: ```www.helloworld.com/get/123, I'll display the content.

router.get('/get/:id', async (req,res) => {
    const q_id = await Excuse.findById({_id: req.params.id})
    res.json(q_id)

What I'm stuck with is, how do I get the api using the type? I tried to stringify but it doesn't seem to work.


Solution

  • I didn't understand exactly, but maybe you are talking about a query.

    GET /get/:id?type=some-type
    

    The route would be like

    router.get('/get/:id', async (req,res) => {
        const type = req.query.type; // that is "some-type"
        
        const q_id = await Excuse.findOne({ type: type })
        res.json(q_id)