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?
CodePudding user response:
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)
}
})