猫鼬分组依据(将标头发送到客户端后无法设置标头)

时间:2020-03-18 23:31:17

标签: javascript node.js mongodb mongoose mongoose-schema

我的SQL查询如下

选择* 从日志 其中type_of('success')和id = 3 按名称分组

我的架构如下:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;




log_schema = new Schema (
    {  

        name     : {type: String},

        type_of            : {type: String, enum:['recordsadd','missed'], required: true},
        // created_time    : {type: Date, default: Date.now}  
    }
);



//Export model
module.exports = mongoose.model('log', log_schema);


我的控制器如下:

exports.logdata = (req, res, next) => {
  const errors = validationResult(req); 
  if (!errors.isEmpty()) {
      res.status(messages.status.validationError).json({ errors: errors.array() });
      return next(errors);
  }

  const  name =req.query.;
  const  type_of =req.query.type_of;

  const list = [];

  unique_records(name,type_of)
  .then(result => {

    if (result.length > 0){

      for(i=0; i<result.length; i++){
        list[i]={}

        list[i]['type_of']=result[i].type_of
        list[i]['name']=result[i].name

    }
    // Displays the message
     res.status(messages.status.OK).json(list);   

  }
    return;
  })

  .catch(function (err) {
    res.status(messages.status.serverError).json({ errors: err });
      return next(errors);

  });

}


const unique_records = (name,type_of) => {

  return new Promise(function(resolve, reject) {

    local_lr_urllog.
    aggregate([ 
      {$match : { type_of : {$in:[type_of]}}},
      {
      $group:
          {
            _id:name
//If I insert $sum:1 here I get a count of my output as 3
          }
  },

  ]).exec(function(err,result){
        if(err){

          reject(err);
      }
      else{

       //When I try to print my result here I get only 1 array. But actually there are 3 results.
        resolve(result);
      }
       });

});
}

我的结果查询有3个输出。但是我在邮递员的输出上空白。我是否在我的promise函数中的unique_records中缺少某些内容。当我打印出决心时,我只会得到一个结果,而不是三个结果。

0 个答案:

没有答案