【发布时间】:2020-09-06 19:20:10
【问题描述】:
我使用 MongoDB 作为数据库并使用 express.js 来构建 API。 我想在将响应发送给客户端之前对其进行修改。 这是我的 express.js 代码...
products.route("/:id")
.get((req, res) => {
mdb.get().collection('products').find({_id:parseInt(req.params.id), status:1}).toArray()
.then(response => res.status(200).json(response))
.catch(error => console.error(error));
})
例如,我想在响应中添加一些计算字段。假设我来自数据库的响应对象是这个
response = {
id: 1001,
name: 'Apple',
price: 120
}
现在,我想添加图像字段。所以,我的最终响应对象将是
response = {
id: 1001,
name: 'Apple',
price: 120,
image: '/assets/images/'+id+'.jpg'
}
请帮我解决这个问题,我是 express.js 的新手
【问题讨论】:
标签: node.js mongodb api express