Array has value but the length of the array is 0 when I do console log

Viewed 28

I am new to electron and I am trying to build a simple CRUD application for managing student. I am using sqlite3 as the database. The issue is, when I query a single student from the database based on the id and push the result into an empty array and do console log, the array appears to be empty until I press the destructure triangle on the left of the array before i can view the content but still i cannot access the content of the array.

The link to the screenshot of my console is below;

[ [1]: https://i.stack.imgur.com/3U7mf.png

Below is the databse query

class Controller{
getStudentById(id) {
const sql = `SELECT * FROM students WHERE id = ?`;
let res = [];

db.get(sql, [id], (err, rows) => {
  if (err) {
    throw err;
  }

  res.push(rows);
});
return res;
}
}

module.exports = Controller;

Code in my preload script

const Controller = require("../models/controller");
const studentCtl = new Controller();

const findStudentById = (id) => {
   const student = studentCtl.getStudentById(id);
   console.log(student); // This where I did my console log
   return student;
};

I have been on this issue for a while now and would really appreciate any help.

0 Answers
Related