【问题标题】:Date Range Query in Mongoose Converting circular structure to JSONMongoose中的日期范围查询将循环结构转换为JSON
【发布时间】: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


【解决方案1】:
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 });
            }
      })

【讨论】:

  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 2021-03-05
  • 2021-01-02
  • 1970-01-01
  • 2016-01-11
  • 2017-04-03
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多