[英]I can't get any query result Node.js MongoDB
This is my code:这是我的代码:
const getAllUsageHistoryByDate = async (user_id, date) => {
const mDate = moment.utc(date).startOf('day');
console.log(mDate.toDate(), mDate.endOf('day').toDate(), user_id);
return UsageHistory.find({ "reminder.user_id": user_id, created_date: { $gte: mDate.toDate(), $lte: mDate.endOf('day') }, is_active: true });
}
This is the console.log output:这是console.log output:
2022-08-19T00:00:00.000Z 2022-08-19T23:59:59.999Z 62ed199fb9584c9e88dffa10
When I execute this code in my program, function returns an empty list ([]).当我在我的程序中执行此代码时,function 返回一个空列表 ([])。 But if I execute it from MongoDB Compass with the filter below, it shows 8 results:
但是如果我使用下面的过滤器从 MongoDB Compass 执行它,它会显示 8 个结果:
{ "reminder.user_id": '62ed199fb9584c9e88dffa10', created_date: { $gte: ISODate('2022-08-19T00:00:00.000Z'), $lte: ISODate('2022-08-19T23:59:59.999Z') }, is_active: true }
If I delete the "created_date" part of the filter from my code, it works but I need that part.如果我从我的代码中删除过滤器的“created_date”部分,它可以工作,但我需要那个部分。 Why can't I get any results from my code?
为什么我不能从我的代码中得到任何结果?
Maybe you are missing await
也许你错过了
await
const getAllUsageHistoryByDate = async (user_id, date) => { return await UsageHistory.find(....); } // or const getAllUsageHistoryByDate = async (user_id, date) => { return UsageHistory.find("...").then(result => {"..."}).catch(err =>{"..."}); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.