On my node JS Server I have my module file. My module files connects to a DB and pulls a list of rows. I am exporting a function that returns an array of objects. Basically Iw ant to return the information I got from the DB and pass it back to my main script to do something with it.
So this is how my module script looks like
-- mymodule.js --
function smsdcontact(fname, lname, smsdcell, smsdemail) {
this.fname = fname;
this.lname = lname;
this.smsdcell = smsdcell;
this.smsdemail = smsdemail;
}
var smsdgetlist = function (smsdgroupid) {
var smsdlist = [];
connection.connect();
connection.query('SELECT * from mymembers', function(err, rows, fields) {
if (!err) {
console.log('The number of rows found are: ', rows.length);
for (i = 0; i < rows.length; i++) {
smsdlist.push (new smsdcontact(rows[i].memfname,rows[i].memlname,rows[i].memcell,rows[i].mememail));
}
connection.end();
console.log(smsdlist);
return smsdlist;
}
else
console.log('Error while performing Query.'+err);
});
}
exports.smsdgetlist = smsdgetlist;'
My main script would look like that
-- main.js --
var test = require("./smsdsql.js");
var returnedarray = test.smsdgetlist("0");
console.log(returnedarray)
The output looks like that
-- Output --
C:\node main.js
undefined
The number of rows found are: 2
[ smsdcontact {
fname: ‘user 1 first name',
lname: ‘use 1 last name',
smsdcell: ‘user 1 cell phone',
smsdemail: ‘user 1 email },
smsdcontact {
fname: 'user 2 first name'',
lname: 'user 2 last name',
smsdcell: 'user 2 cell phone',
smsdemail: ‘user 2 email' } ]
So the issue is that It sounds that the return is undefined