Skip to content

Instantly share code, notes, and snippets.

@bramses
Created December 17, 2021 08:33
Show Gist options
  • Select an option

  • Save bramses/d14d620c4135e6ec0d9797fd725da029 to your computer and use it in GitHub Desktop.

Select an option

Save bramses/d14d620c4135e6ec0d9797fd725da029 to your computer and use it in GitHub Desktop.
Putting the RUD in CRUD (NextJS + Firebase)
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