How to use groupBy in js Bookshelf?

how to use groupBy in the JS Bookshelf , here is my controller code.

router.route('/fetchStudentAttendance')
.post(function(req, res) {
 StudentAttendance
  .query(function (qb){
    qb.where('date', '>=', req.body.date);
    qb.groupBy("students_id");
  })
.where({'class_id': req.body.class_id, 'section_id': req.body.section_id})
.fetchAll({ withRelated: [{'StudentRef':function(qb) {qb.column('id', 'first_name', 'middle_name', 'last_name')}}]})
.then(studentAttendance => {
  let content = {
    data: studentAttendance,
    success: true,
    message: 'Record Not Found',
  };
  return res.send(content);
})
.catch(error => {
  let content = {
      data: error,
      success: false,
      message: 'Error while fetching Student Attendance.',
    };
  return res.send(content);
});

});

when I try "groupBy" employee_id it will give an error similar to this.

code:"42703"
file:"parse_relation.c"
length:110
line:"3194"
name:"error"
position:"127"
routine:"check_ungrouped_columns_walker"
severity:"ERROR"
+6
source share
1 answer

TL DR

Use the bookshelf's built-in collection manipulation to do custom groupByafter .fetchAll() OR Use raw Knex to generate your query and results as needed, as groupBy will need some SQL aggregation definitions. This may be the result of a Knex query or a Bookshelf object.

Other words

; libray KnexJS . . SQL "groupBy" , lodash, lodash groupBy . , , SQL .

Model new Model Model.forge() , ( , .fetchAll()). Knex, Model.query (. Docs), , Bookshelf. .query(function (qb) {}) knex, , WHERE qb.orderBy('prop'). groupBy "", Model.query().fetchAll() ( , , , , , ). Knex .

+3
source

All Articles