首页 > 解决方案 > CastError:在路径的起息日转换为日期失败

问题描述

我正在尝试在我用 Mongo 开发的数据库中进行查询,我们有模式 Examen(或考试),我们需要找到两个日期之间的所有考试。我的系统可以获取两个日期并使用此表单进行查询(我也使用 Moment 以获得正确的格式)

这是我从网站上得到的查询

{ "examDate": { "$gt": { "$date": "1556686800000" }, "$lt": { "$date": "1559192400000"} } }

当我在 Studio3T 中运行此查询时,它可以正常工作,但在网站中出现错误:

CastError:模型“Examen”的路径“examDate”处值“{'$date':'1559192400000'}”的转换为日期失败

我也尝试使用 ISO 格式更改“$date”格式,但没有成功。奇怪的是,查询在 Studio3T 中正常工作,但使用 Mongo db.find() 函数却不能这样工作。

//this is how I make the find function for my schema
Examen.find((query), (err, info_examen) => {
    if (err) return res.status(500).send({ message: `Error: ${err}` })
    if (!info_examen) return res.status(404).send({ message: `there is no data` })
    res.status(200).send({ info_examen })
})

我希望得到结果(info_examen)以便对数据进行统计。

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读