Mon goose group by (Невозможно установить заголовки после их отправки клиенту) - PullRequest
0 голосов
/ 19 марта 2020

Мой sql запрос, как показано ниже

выберите * из журнала, где type_of in ('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 выхода. Но я получаю пустым как вывод на почтальона Я что-то упустил в своей функции обещания на unique_records. Когда я печатаю с разрешением, я получаю только один результат вместо трех.

...