Express+Mongoose格式化响应以发送



我是javascript的新手,不知道如何按照我想要的方式格式化响应。

这是我的api的代码:

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

我想将响应发送为:

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

但这就是它的发送方式:

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

如何删除响应对象中的外层?

简单地不使用文档密钥创建对象,而是直接将其传递给

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

最新更新