Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1013B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const Person = require('../../../models/person');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'GET': {
  8. try {
  9. const dataCount = await Person.countDocuments();
  10. if (dataCount === 0) {
  11. res.status(200).json({
  12. message: 'No data.',
  13. dataIds: [],
  14. });
  15. break;
  16. }
  17. const dataFromDB = await Person.find({}).limit(4);
  18. if (!dataFromDB) {
  19. throw new Error('No data!');
  20. }
  21. const dataIds = dataFromDB.map((item) => item.customID);
  22. res.status(200).json({
  23. message: 'Fetch ids successfull!',
  24. dataIds: dataIds,
  25. });
  26. } catch (error) {
  27. res.status(400).json({ message: error.message });
  28. }
  29. break;
  30. }
  31. default:
  32. res.status(405).json({ message: 'Method not allowed' });
  33. break;
  34. }
  35. }
  36. export default handler;