![](/img/trans.png)
[英]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.