node.js - 无法使用 findOne() 通过 id 在 mongodb 中找到子文档
问题描述
检查状态 200 和状态 400 的测试用例失败。在这两种情况下,我都收到了 404 状态,这意味着租借在 db 中不存在。
const rental_exists = await RentalCollectionClass.findOne({
"customer._id" : req.body.customerId,
"movie._id" : req.body.movieId });
if(!rental_exists)
return res.status(404).send('Rental with given customer/movie not in db');
if(rental_exists.dateReturned)
return res.status(400).send('Rental with given customer/movie already processed');
return res.status(200).send('Valid rental posted');
但是我在测试用例的 beforeEach() 函数中创建了租用,如下所示。
beforeEach(async () => {
customerId = mongoose.Types.ObjectId();
movieId = mongoose.Types.ObjectId();
rental = new RentalCollectionClass({
customer : {
id : customerId,
name : 'nandhini',
phone : 12345
},
movie : {
id : movieId,
title : 'movieTitle',
}
});
await rental.save();
});
另请注意,客户和电影是租赁的子文件。请帮我解决这个问题!!
解决方案
推荐阅读
- ios - regionDidChange 中的计时器不会快速触发 4
- docker - 如何从 github 拉取 docker 镜像并在 ec2 中构建镜像?
- sql-server - 根据列而不是整行删除重复项
- c++ - 如何根据用户输入将对插入到地图中,然后找到与用户给定的键关联的元素?
- jquery - JQuery UI自动完成+标签插件(XOXCO):选择时不添加标签
- android - Android:将不同的内容从视图模型传递到不同的包含标签
- javascript - 获得 200 ok 状态但从邮递员那里给出响应错误
- php - PHP 和 MYSQL 中的错误 1064
- php - angularjs GET 500(内部服务器错误)
- python - 在 Excel 中打开 Python 写入 CSV