0

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

m_callens
  • 6,100
  • 8
  • 32
  • 54
Joe
  • 1
  • Your output has an interesting mix of quotes. There's single quotes, double quotes, typographic quotes... There is absolutely no way that you have copied that from an actual window that shows your actual output. – Tomalak Feb 16 '17 at 17:47
  • Sorry @m_callens there extra quotes as typo as I was trying to mask the real data – Joe Feb 16 '17 at 17:49
  • Mask the real data by changing the *data*, not by changing the output. – Tomalak Feb 16 '17 at 18:00

1 Answers1

1

EDIT

Duplicate of Wait until fs.readFileSync is done - exactly the same logic.


You are performing asynchronous operation, so you can't just assign it's result to a variable. What you can do is to add callback parameter, which would be a function, to smsdgetlist function and call it with the result, which would be smsdlist in your case

function smsdgetlist(smsdgroupid, callback){
    connection.connect();
    connection.query(query, function(err, rows, fields){
        for (i = 0; i < rows.length; i++) {
            smsdlist.push (new smsdcontact(rows[i].memfname,rows[i].memlname,rows[i].memcell,rows[i].mememail));
        }
        // after pushing all elements to desired variable, use the callback
        connection.end();
        callback(smsdlist);
    });
}

And what you would do in main.js would be

var test = require("./smsdsql.js");
test.smsdgetlist("0", function(result){
    console.log(result); // here you would obtain the smsdlist array from smsdgetlist function
});
Community
  • 1
  • 1
piotrbienias
  • 7,201
  • 1
  • 28
  • 33