I need help with Next.js api
ivkeMilioner

ivkeMilioner @ivkemilioner

Location:
Beograd
Joined:
Oct 15, 2019

I need help with Next.js api

Publish Date: Mar 28 '22
4 3

I want to change "collections" (string) from front end? How to do that?

Image description

Comments 3 total

  • Jan Vlnas
    Jan VlnasMar 28, 2022

    Hey @ivkemilioner, for starters I recommend tagging your post with help to reach more people.

    Anyways, if I understand your question correctly, you want to pass collections as an outside parameter to your API endpoint? I recommend checking the documentation, you can use query parameter to pass data to your endpoint.

    For example, if you access your API route like this: /api/qapages?collection=questions, you can read the collection query parameter via req.query:

    export default async function qaPages(req, res) {
      const collection = req.query.collection;
      //...
      const questions = await db
         .collection(collection)
      // ...
    }
    
    Enter fullscreen mode Exit fullscreen mode

    However, make sure to validate the incoming parameter so it's possible to access only collections which should be accessible this way.

  • rohit20001221
    rohit20001221Mar 28, 2022

    hi @ivkemilioner i have a suggestion 😁 you can use singleton pattern to create a one time connection to database and then reuse that connection so that you need not call connectToDatabase over and over

Add comment