【发布时间】:2021-08-20 16:25:57
【问题描述】:
我在转换为 momentJS 然后在数据库中搜索后遇到了这个问题。它在 catch 块中向我显示以下错误
将循环结构转换为 JSON\n --> 从对象开始 构造函数 'NativeTopology'\n |属性 's' -> 对象 构造函数'对象'\n |属性 'sessionPool' -> 对象 构造函数 'ServerSessionPool'\n --- 属性 'topology' 关闭 圆圈
const getAppointmentByDate = async(req, res, next) => {
try {
//get dates from req.query by es6 object destructuring
//1. check that date is not empty
//2. check that date is in the right format
//expected result: YYY-MMM-DDD
reqDate = new Date("1-1-2021")
var today = moment(new Date()).format('YYYY-MM-DD[T00:00:00.000Z]');
console.log("Next day -- " + (reqDate.getDate() + 1))
var d = new Date();
d.setDate(reqDate.getDate() + 1);
var tomorrow = moment(d).format('YYYY-MM-DD[T00:00:00.000Z]');
//In some cases you'll get a date-time format where you have to separate the date
//from the time.
const transactions = Appointment.find({
"schedule_datetime": {
"$gte": new Date(today),
"$lt": new Date(tomorrow)
}
})
//4. Handle responses
if(!transactions) {
return res.status(404).json({
status:'failure',
message:'Could not retrieve transactions'
})
}
res.status(200).json({
status:'success',
data: transactions
})
} catch(error) {
return res.status(500).json({
status:'failure',
error: error.message
})
}
}
【问题讨论】:
-
你能告诉你代码的哪一行,这个错误是从你的代码中发出的吗?通过在每条可疑行之前插入
console.log(1)来找出它,并检查该行何时没有被安慰。我估计在cons transcations = ....之前插入它 -
您创建
Date并来回转换为字符串。为什么不简单地schedule_datetime: {$gte: moment().startOf('day').toDate(), $lt: moment().startOf('day').add(1,'day').toDate() }? -
new Date("1-1-2021")格式错误。必须是new Date("2021-01-01"),见ISO-8601
标签: javascript node.js mongodb mongoose mongoose-schema