Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将2个日期传递给req参数以查找2个日期之间的数据_Node.js - Fatal编程技术网

Node.js 如何将2个日期传递给req参数以查找2个日期之间的数据

Node.js 如何将2个日期传递给req参数以查找2个日期之间的数据,node.js,Node.js,我正在为我的公司创建一个用于报告的RESTAPI 路由传递日期以查找2个给定日期的数据 控制器操作以获取日期之间的数据 在邮递员中传递参数时,我得到的所有报告与参数中给出的日期无关 您不需要使用$where条件,只需在find函数中设置请求对象即可 我认为您应该使用$gte和$lte运营商,而不是$between(cf) 以下是一个例子: const dcr = await Dcr.find({ "createdAt": { "$gte": new Date(curre

我正在为我的公司创建一个用于报告的RESTAPI 路由传递日期以查找2个给定日期的数据

控制器操作以获取日期之间的数据

在邮递员中传递参数时,我得到的所有报告与参数中给出的日期无关

  • 您不需要使用$where条件,只需在find函数中设置请求对象即可

  • 我认为您应该使用$gte和$lte运营商,而不是$between(cf)

  • 以下是一个例子:

    const dcr = await Dcr.find({
        "createdAt": {
            "$gte": new Date(currentDate),
            "$lt": new Date(lastDate)
        }
    });
    
    编辑

    如果它仍然不起作用,可能是因为您的日期没有格式化,也许您应该使用一个日期库,如,并尝试如下:

    const moment = require('moment');
    const dcr = await Dcr.find({
        "createdAt": {
            "$gte": new Date(moment(currentDate, 'YYYY/MM/DD').format()),
            "$lt": new Date(moment(lastDate, 'YYYY/MM/DD').format())
        }
    });
    

    希望能有所帮助。

    如果至少对日期使用查询字符串而不是查询参数,效果会更好。在您提供的路线中:

    router.get('/smDcr/:currentDate?&:lastDate:', authorize, getDcrBetweenDates);
    
    在url中使用?&:传递第二个日期值将被解释为查询字符串,因为()。 您通过调用以下命令来访问此端点:

    http://localhost:3000/smDcr/?currentDate=2019/09/11&?lastDate=2019/09/11
    
    在这种情况下,currentDate将被视为查询字符串,lastDate

    因此,我建议您改用这个:

    router.get('/smDcr', authorize, getDcrBetweenDates);
    
    在控制器中,访问如下值:

        const lastDate = req.query.lastDate;
        const currentDate = req.query.currentDate;
    
    要访问它,您应该拨打:

    http://localhost:3000/smDcr?currentDate=2019/09/11&lastDate=2019/09/11
    
    这是一份正式的路由文件。

    很抱歉回复晚了。谢谢大家的支持。它起作用了,不客气你能把我的答案标记为已解决吗?
        const lastDate = req.query.lastDate;
        const currentDate = req.query.currentDate;
    
    http://localhost:3000/smDcr?currentDate=2019/09/11&lastDate=2019/09/11