DEV Community

ivkeMilioner
ivkeMilioner

Posted on • Edited on

How to dynamically change collection in from client side ?

I want to change collection from client side, but I do not have sucess.

import clientPromise from '../../lib/mongodb2'

export default async function handler (req, res) {

  const collection = req.query.collection
  const client = await clientPromise


  const db2 = client.db('javascript_questions')
  switch (req.method) {
    case 'GET':
      const question = await db2
        .collection(collection)
        .find({})
        .toArray()
      res.json(question )
      break
  }
}
Enter fullscreen mode Exit fullscreen mode

component

export async function getStaticProps (context) {
  let res1 = await fetch('http://localhost:3000/api/questionApi', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json'
    }
  })
  let question = await res1.json()

  return {
    props: { question }
  }
}

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more