【发布时间】:2020-08-01 18:15:30
【问题描述】:
我是猫鼬和节点的新手,谁能指出哪种方法是正确的,我的目的是将我的 MongoDB 中所有产品的列表发送给客户端。
方法一
items = await Product.findById({})
.then((items) => {
if (items == null) {
res.status(404).send("no items");
} else {
res.send(items);
next();
}
})
.catch((err) => {
res.status(404).send({ error: err });
});
方法二
items = await Product.find({}).exec();
res.status(200).send(items)
next();
如果我必须处理项目并在此之后发送,您能否指出我如何再次使用等待呼叫。提前致谢。
【问题讨论】:
标签: node.js mongodb mongoose async-await