Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJS API directing to index.js file instead of [id].js

I’m setting up an API in NextJS. Currently, I have a folder of routes /classes. In this folder, I have index.js and [id].js. 1

The purpose of /classes/ is to get all the classes in the db or post a new class.
The purpose of /classes/[id] is to get the specific class, edit the class, or delete it. My issue is that when I try to get a class by id, its returning all the classes from the index.js file. Any ideas about what I’m missing?

index.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if (req.method === 'GET') {
  try {
    console.log('in index get')
    const allClasses = await db.collection('classes').orderBy('name').get()
    const classes = allClasses.docs.map((doc) => {
      return {
        id: doc.id,
        ...doc.data()
      }
    })
    res.status(200).json(classes)
  } catch (error) {
    res.status(400).json({
      error: error.message
    })
  }
} else if (req.method === "POST") {
  try {
    await db.collection('classes').add({
      ...req.body,
      lastUpdated: new Date().toISOString()
    })
    res.status(200).json({
      message: 'New Class Created'
    })
  } catch (e) {
    res.status(500).json({
      error: 'There was an error adding class'
    })
  }
}

[id].js

switch (method) {
  case 'GET':
    try {
      console.log('in id get')
      const classById = await db.collection('classes').doc(id).get().data()
      res.status(200).json(classById)
    } catch (error) {
      res.status(400).json({
        error: `Class does not exist`
      })
    }

    break;
  case 'DELETE':
    try {
      await db.collection('classes').doc(id).delete()
      res.status(200).json({
        message: 'Class Deleted Successfully'
      })
    } catch (error) {
      res.status(400).json({
        error: `Class does not exist`
      })
    }
    break;
  case 'PUT':
    try {
      await db.collection('classes').doc(id).update({
        ...req.body,
        lastUpdated: new Date().toISOString()
      })
      res.status(200).json({
        message: 'Class Updated Successfully'
      })
    } catch (error) {
      res.status(400).json({
        error: `Class does not exist`
      })
    }
    break;
  default:
    res.setHeader('Allow', ['GET', 'PUT', 'DELETE'])
    res.status(405).end(`Method ${method} Not Allowed`)
    break;
}

>Solution :

From your comment

{{localhost}}/api/classes/?id=DSGY9jjuY43M210uFg1U

Try doing this instead:

{{localhost}}/api/classes/DSGY9jjuY43M210uFg1U

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading