在POST之后表达jsMongoDBreturn对象
Express js MongoDB return object after POST
如何在 Express js 中从数据库取回对象?当我执行 POST 请求时,我只返回状态 201 而不是响应中的对象。
下面的方式returns一个空的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());
在mongoDB insertOne
方法中 returns 包含 acknowledged
作为 true
和当前插入的 id (ObjectId) 作为 insertedId
的文档.因此,您可以将来自 mongoDB 的响应存储在变量中,如果发现任何 insertedId
,您可以从 mongoDB 查询数据或从请求正文准备数据。
...
const insertion = 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()
});
let data = {};
if (insertion.acknowledged) {
// ... prepare the data
data = await websites.findOne({_id: insertion.insertedId});
}
...
res.send(data);
希望有用!
好的你可以这样试试
try{let websites = await loadWebsitesCollection.insertOne({
title: req.body.title,
url: req.body.url,
cms: req.body.cms,
fw: req.body.fw,
user: req.body.user,
createdAt: new Date() });;
res.send(websites );}catch(e){res.status(400).send(e)}
或者这样
try{var websites = new loadWebsitesCollection({
title: req.body.title,
url: req.body.url,
cms: req.body.cms,
fw: req.body.fw,
user: req.body.user,
createdAt: new Date()})var reswebsites = await websites .insertOne(); res.send(reswebsites );}catch(e){res.status(400).send(e)}
如何在 Express js 中从数据库取回对象?当我执行 POST 请求时,我只返回状态 201 而不是响应中的对象。
下面的方式returns一个空的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());
在mongoDB insertOne
方法中 returns 包含 acknowledged
作为 true
和当前插入的 id (ObjectId) 作为 insertedId
的文档.因此,您可以将来自 mongoDB 的响应存储在变量中,如果发现任何 insertedId
,您可以从 mongoDB 查询数据或从请求正文准备数据。
...
const insertion = 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()
});
let data = {};
if (insertion.acknowledged) {
// ... prepare the data
data = await websites.findOne({_id: insertion.insertedId});
}
...
res.send(data);
希望有用!
好的你可以这样试试
try{let websites = await loadWebsitesCollection.insertOne({
title: req.body.title,
url: req.body.url,
cms: req.body.cms,
fw: req.body.fw,
user: req.body.user,
createdAt: new Date() });;
res.send(websites );}catch(e){res.status(400).send(e)}
或者这样
try{var websites = new loadWebsitesCollection({
title: req.body.title,
url: req.body.url,
cms: req.body.cms,
fw: req.body.fw,
user: req.body.user,
createdAt: new Date()})var reswebsites = await websites .insertOne(); res.send(reswebsites );}catch(e){res.status(400).send(e)}