Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Return just single Object instead of Array in NEXT.JS and MongoDB

How can i return single object for instead of array? Because i asking in api just for single "Event" I using MongoDB + Next.js

I always getting

[{}]

i Just want it get

{}

const fetchWithId = (url: RequestInfo) => fetch(url).then((r) => r.json());

const index = () => {
  const router = useRouter();

  const { data, error } = useSWR(
    `http://localhost:3000/api/events/event?slug=${router.query.slug}`,
    fetchWithId
  ); 

Here is a Next API

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  const {
    method,
     query: { slug},
  } = req;

  if (req.method !== 'GET') {
    return;
  }



  await dbConnect();

  const selectedEvent = await Events.find({
    slug: slug,
  });

  res.status(200).json(selectedEvent);
};



export default handler;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You would want to call the method .findOne() rather than .find():

const selectedEvent = await Events.findOne({
    slug: slug
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!