【发布时间】:2021-07-26 03:39:27
【问题描述】:
我正在尝试从节点中的数据库中获取特定的集合文档。我似乎正在检索我的“照片”集合中的整个文档列表。我已经玩过几次了,想知道是否有更简单/更好的方法来获取数据,而不是我正在尝试做的事情。我要做的就是获取一张带有特定 ID 的照片,然后将该照片返回给用户。
这是我的代码:
async function getPhoto(id) {
const db = getDbReference();
const collection = db.collection("photos")
console.log({"id": id})
const results = await collection
.findOne(id)
.toArray();
return results
}
exports.getPhoto = getPhoto;
照片端点文件:
router.get('/:photoID', async (req, res, next) => {
// const photoID = parseInt(req.params.photoID);
try {
const photo = await getPhoto(req.params.photoID)
res.status(201).send(photo);
}catch(err){
console.error("--error", err);
res.status(500).send({
err: "Error fetching photo from DB. Try again later",
});
【问题讨论】: