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

120
Views
Express + Mongoose formatting response to send

I'm new to javascript and not sure how exactly to format responses how I want.

Here is the code for my api:

router.get("/categories", (req, res, next) => {
    try {
        Category.find()
        .then(documents => {
            res.status(200).json({
                documents
            })
        });
    } catch (e) {
        console.log(e)
    }
})

I want to send the response as:

[
   {data},
   {data},
   {data}   
]

But this is how it's sending:

{
   documents: [
      {data},
      {data},
      {data}
   ]
}

How do I remove that outer layer in the response object?

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

0

simply don't create an object with a document key but pass it down directly

router.get("/categories", (req, res, next) => {
    try {
        Category.find()
        .then(documents => {
            res.status(200).json(documents)
        });
    } catch (e) {
        console.log(e)
    }
})
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!