【发布时间】:2019-08-08 19:18:10
【问题描述】:
如何在 Express js 中从数据库中取回对象?当我发出 POST 请求时,我只得到状态 201 而不是响应中的对象。
它下面的方式返回一个空的 res.data 字段而不是对象。
router.post('/', async (req, res) => {
const websites = await loadWebsitesCollection();
await websites.insertOne({
title: req.body.title,
url: req.body.url,
cms: req.body.cms,
fw: req.body.fw,
user: req.body.user,
createdAt: new Date()
});
//TODO Need to get the response from the post request
res.status(201).send();
res.status(404).send('Sorry, we cannot find that!');
res.status(500).send({ error: 'something blew up' });
})
要将所有对象返回到数组中,我可以这样做:
res.send(await websites.find({}).toArray());
【问题讨论】:
标签: javascript mongodb express crud