I am trying to generate a csv report which is later mailed to the recipient. The CSV is generated by using node-query
builder ORM. The resulting CSV is perfectly generated in an environment with my server on Heroku+ClearDB SQL but fails with the error
TypeError: Cannot read property 'length' of undefined
The code portion is as follows:
db.qb.order_by('school_order.school_id', 'desc').distinct()
.select_avg('school_order.student_quality_price', 'average_price')
.select('student_quality.student_quality_name, school_order.unit, school_order.marks_percent,' +
'school_order.fee_amount, school_order.total_marks, school_order.total_value, school_order.po_created_at')
.where({ 'school_order.student_quality_id': matArray, 'school_order.teacher_id': supArray })
.from('school_order')
.join('student_quality', 'school_order.student_quality_id=student_quality.student_quality_id')
.join('supplier', 'supplier.teacher_id=school_order.teacher_id').group_by(' student_quality.student_quality_name').get(function (err, poRes) {
var outputpo = []
console.log("PORESS" + poRes)
if (poRes.length > 0) {
poRes.forEach(po => {
var poDate = new Date(po.po_created_at.toLocaleString()).getTime();
if (poDate >= startDate.getTime() && poDate <= endDate.getTime()) {
outputpo.push(po);
}
});
}
EDIT Database Connection
var db_settings = {
user: `user`,
password: `password`,
database: `database`,
port:port#,
socketPath: `INSTANCE_STRING`
};
var qb = require('node-querybuilder').QueryBuilder(db_settings, 'mysql', 'single');
module.exports.qb = qb;
qb
here is the querybuilder variable. In my logs, console.log("PORESS" + poRes)
returns "PORESSUndefined".
Solved The issue was with the GROUP BY clause which some SQL versions make mandatory to include aggregated columns post the GROUP BY
clause. More details here: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.