Advertisement
digininja

Nodejs SQL scope

Mar 8th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. I want this code to return the array of students that is generated by going through the SELECT statement.
  3. Due to db.each going off on its own thread, the `students` array it populates is in a different scope
  4. to the one I declare which is returned empty before the db.each finishes.
  5.  
  6. How do I get the `return` to return the data?
  7. */
  8.  
  9.  
  10.     searchStudents: ({term}) => {
  11.  
  12.         var lessons = ['maths', 'English'];
  13.         var students = [];
  14.  
  15.         console.log (students);
  16.             db.each(`SELECT * FROM students`, (err, row) => {
  17.                 if (err) {
  18.                     console.error(err.message);
  19.                 } else {
  20.                     student = new Student (row.id, row.name, lessons, row.password, row.attendence);
  21.                     students.push (student);
  22.                     console.log(row.id + "\t" + row.name + "\t" + row.password);
  23.                 }  
  24.             });
  25.         console.log (students);
  26.  
  27.         return students;
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement