使用日期:提供的值不是ISO格式-Node.js and Moment.js
我正在使用Moment.js将字符串从req.query转换为new Date()对象,并在mongo.db聚合$ GTE和$ LT中使用它(经典搜索按日期范围)。在基础上,我将日期存储为日期(格式yyyy-mm-dd - >示例:构造: 2022-06-06-06T07:00:00.000+00:00:00
)进入我的控制台和错误:折旧警告:提供的值不在已公认的RFC2822或ISO格式中。时刻的构造落后于JS Date(),在所有浏览器和版本中都不可靠。不建议使用非RFC2822/ISO日期格式。
我的API-后端代码是: 我做过的iSvalid()函数是因为,有时我只选择startdate,然后它为我提供了无效的enddate 的日期
router.get("/searchDate", async (req, res) => {
try {
function isValid(data: any) {
if (data == "Invalid Date") {
return new Date(moment("2100-11-11"));
} else {
return data;
}
}
const { startDate, endDate } = req.query;
const containers = await Container.aggregate([
{
$match: {
$or: [
{
manufacturingDate: {
$gte: isValid(new Date(moment(startDate))),
$lt: isValid(new Date(moment(endDate))),
},
},
],
},
},
]);
res.status(200).json(containers);
} catch (err) {
res.status(404).json({ success: false, msg: "Container not found" });
}
});
。 :2022-06-06,但是如果我只从中选择,那么此问题是因为我发送了一个endDate = undefine define to to我的API。
代码是:
onChangeDate() {
function convert(str: any) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
this.containersService
.getContainersByDateRange(convert(this.from), convert(this.to))
.subscribe((containers: any) => {
this.containers = containers;
});
}
我知道我无法转换不确定,这就是为什么我会出现此错误。有一些解决方法吗?
I am using Moment.js to transform string from req.query to new Date() object and use it in mongo.db aggregation $gte and $lt (classic search by date range). In the base I store date as Date (format yyyy-mm-dd -> example: manufacturingDate: 2022-06-06T07:00:00.000+00:00)
The problem is, that I am getting in my console and error: Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged.
My api - backend code is:
The isValid() function I did because, sometimes I choose only startDate and then it gives me Invalid date for endDate
router.get("/searchDate", async (req, res) => {
try {
function isValid(data: any) {
if (data == "Invalid Date") {
return new Date(moment("2100-11-11"));
} else {
return data;
}
}
const { startDate, endDate } = req.query;
const containers = await Container.aggregate([
{
$match: {
$or: [
{
manufacturingDate: {
$gte: isValid(new Date(moment(startDate))),
$lt: isValid(new Date(moment(endDate))),
},
},
],
},
},
]);
res.status(200).json(containers);
} catch (err) {
res.status(404).json({ success: false, msg: "Container not found" });
}
});
In the req.query I pass the date in format (yyyy-mm-dd), example: 2022-06-06, BUT if I chose only FROM, then this problem occurs because I send an endDate = undefined to my API.
Code is:
onChangeDate() {
function convert(str: any) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
this.containersService
.getContainersByDateRange(convert(this.from), convert(this.to))
.subscribe((containers: any) => {
this.containers = containers;
});
}
I know that I can't convert undefined and thats why I than get this error. Is there some workaround trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论