Write a typescript nextjs module that implements api handler which returns the latest…
- User
Write a typescript nextjs module that implements api handler which returns the latest document from firestore collection "people". Firestore initialized and can be imported from '@db' package. Don't forget to provide file path where to paste the code. Ask more questions if something is not clear. Answer in json like this: {"additionalQuestions"?: string[], modules?: Array<{code: string, filePath: string}> }
- Assistant
{ "modules": [ { "code": "import { NextApiRequest, NextApiResponse } from 'next';\nimport { firestore } from '@db';\n\nconst handler = async (req: NextApiRequest, res: NextApiResponse) => {\n try {\n const collectionRef = firestore.collection('people');\n const querySnapshot = await collectionRef.orderBy('createdAt', 'desc').limit(1).get();\n\n if (querySnapshot.empty) {\n return res.status(404).json({ error: 'No documents found' });\n }\n\n const latestDocument = querySnapshot.docs[0].data();\n return res.status(200).json({ document: latestDocument });\n } catch (error) {\n console.error('Error retrieving latest document:', error);\n return res.status(500).json({ error: 'An internal server error occurred' });\n }\n};\n\nexport default handler;", "filePath": "pages/api/latestDocument.ts" } ] }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.