Created
December 17, 2021 08:33
-
-
Save bramses/d14d620c4135e6ec0d9797fd725da029 to your computer and use it in GitHub Desktop.
Putting the RUD in CRUD (NextJS + Firebase)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import db from '../../../utils/db'; | |
| export default async function handler (req, res) { | |
| const { id } = req.query; | |
| try { | |
| if (req.method === 'PUT') { | |
| await db.collection('my_collection').doc(id).update({ | |
| ...req.body, | |
| updated: new Date().toISOString(), | |
| }); | |
| } else if (req.method === 'GET') { | |
| const doc = await db.collection('my_collection').doc(id).get(); | |
| if (!doc.exists) { | |
| res.status(404).end(); | |
| } else { | |
| res.status(200).json(doc.data()); | |
| } | |
| } else if (req.method === 'DELETE') { | |
| await db.collection('my_collection').doc(id).delete(); | |
| } | |
| res.status(200).end(); | |
| } catch (e) { | |
| res.status(400).end(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment