【发布时间】:2021-11-28 04:55:06
【问题描述】:
我在 mongodb 数据库中的交易日期格式为“7/1/2021 12:30:48 PM”(字符串为 M/D/YYYY)。在 React 方面,我输入了两个日期:From date 和 To Date,它们都是格式为 YYYY-MM-DD 的字符串。我正在使用下面的代码来查找介于 From 和 To 日期之间的交易。我收到演员表错误,但所有日期都是字符串格式。它只适用于一个没有 $gte 和 $lte 的日期。
错误消息: CastError:模型“事务”的路径“Trandate”中值“{'$regex':'7/1/2021','$options':'$i'}”(类型对象)的值转换为字符串失败
const chooseWinner = await Transaction.find(
{
ContCode: CountryCode,
CurrCode: CurrCode,
CorrOrgCode: CorrorgCode,
ServCode: ServCode,
BranchCode: BranchCode,
Trandate: {$gte: {$regex : SDate, $options: "$i"}},
Trandate: {$lte: {$regex : EDate, $options: "$i"}},
},
"CustomerCode ReferenceNo Trandate "
);
我还把 from 和 to 日期更改为以下格式:
var SDate = new Date(FromDate).toLocaleDateString("hi-IN",{month:'numeric'})+"/"+
new Date(FromDate).toLocaleDateString("hi-IN",{day:'numeric'})+"/"+
new Date(FromDate).toLocaleDateString("hi-IN",{year:'numeric'});
console.log("frmdate: ",SDate);
var EDate = new Date(ToDate).toLocaleDateString("hi-IN",{month:'numeric'})+"/"+
new Date(ToDate).toLocaleDateString("hi-IN",{day:'numeric'})+"/"+
new Date(ToDate).toLocaleDateString("hi-IN",{year:'numeric'});
console.log("enddate: ",EDate);
【问题讨论】:
标签: javascript node.js reactjs regex mongodb