Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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
Javascript 猫鼬排序不';无法使用find()工作_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 猫鼬排序不';无法使用find()工作

Javascript 猫鼬排序不';无法使用find()工作,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,你知道为什么猫鼬查询不起作用吗?我确信这是排序问题,因为当我删除排序部分时,我能够获得一些数据。请尝试此代码 Sales.find({ "email":req.body.email, "createAt": { "$gte": firstDayOfMonth(month), "$lte": lastDayOfMonth(month) }, "sort":{ cre

你知道为什么猫鼬查询不起作用吗?我确信这是排序问题,因为当我删除排序部分时,我能够获得一些数据。

请尝试此代码

Sales.find({
         "email":req.body.email, 
         "createAt": {
         "$gte": firstDayOfMonth(month),
         "$lte": lastDayOfMonth(month)
         },
         "sort":{
            createAt: -1 //Sort by Date Added DESC
         }
        }, function(err,response){
            callback(err,response);
        });

试试这个,它将与ES6一起工作

var queryOptions = {
    $and: [{
        'createAt': {
            "$gte": firstDayOfMonth(month),
            "$lte": lastDayOfMonth(month)

        }
    }, {
        'email': req.body.email
    }]
}
Sales
    .find(queryOptions)
    .sort('-createAt')
    .exec(function(err, response) {
        return res.status(200).json(response);

    }).catch(function(error) {
        return res.status(500).json(error);
    });

protip:清理该电子邮件字段。不能保证直接从
req.body
中导出的字段不会出现格式错误,甚至不会恶意地试图利用别人知道的猫鼬的任何bug,而你却不知道。也就是说:你没有给我们任何信息来帮助你解决你的问题。剩下的给我们看的样本数据在哪里呢?这些数据证明了这一点?(例如,您的数据库中有什么,req.body.email中有什么,您的回调在错误和响应方面看到了什么,等等)@Mike'Pomax'Kamermans您不必知道太多,就可以知道上面的代码出了什么问题,因为我早就说过,如果我删除排序,它会工作。您的假设是完全错误的。这个查找查询并没有什么奇怪的地方,所以请显示足够的代码来构成一个“我不能使用exec()”,因为我使用的是一个中间件调用async。@MariaJane很高兴能帮助您
Sales.find({
     "email":req.body.email, 
     "createAt": {
     "$gte": firstDayOfMonth(month),
     "$lte": lastDayOfMonth(month)
     },
     "sort":{
        [createAt]: -1 //Sort by Date Added DESC
     }
    }, function(err,response){
        callback(err,response);
    });