[英]TypeError: Converting circular structure to JSON in mongoDB/mongoose
[英]Date Range Query in Mongoose Converting circular structure to JSON
转换为momentJS然后在数据库中搜索后,我遇到了这个问题。 它在 catch 块中向我显示以下错误
将循环结构转换为 JSON\n --> 从 object 开始,使用构造函数 'NativeTopology'\n | 属性 's' -> object 与构造函数 'Object'\n | 属性 'sessionPool' -> object 和构造函数 '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
})
}
}
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)
}
})
transactions.each((err, item) => {
//error handling
//console.log(item)
if (err) {
console.log(err)
return res.status(500).json({
isSuccess: false,
data: {},
message: errorcodes.error_500,
status: 500,
});
}
//pushing it into array
if (item != null) {
delete item._id;
result.push(item);
} else {
res.status(200).json({ "data": result });
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.