The code after the return statement (broadcast = row;) is not excecuted.
You should make them switch position if you want to assign the value of row to broadcast. In your comments however you've written that you want the results to be added to the array broadcast. That's why in the provided awnser you'll find it is an array and the row value is added to it.
Also because it runs in async you will need some callback function when the value has been added. Otherwise logging the broadcast array to fast may results in a 'still' empty array.
Broadcast.prototype.broadcast = [];
Broadcast.prototype.add = function (id,cb) {
// Use self or bind the function(err,row) to Broadcast instead so you can use a normal this inside that function as well
var self = this;
mysql.query("SELECT * FROM Table WHERE Id='" + id + "'", function(err, row){
// Check for errors
if (err) {return console.log(err);}
// Add the value of row to the broadcast array
self.broadcast.push(row);
// Run the callback function
cb();
});
};
var broadcast = new Broadcast();
broadcast.add(id, callbackFunction = function(){
// Here broadcast should have a value
console.log(broadcast.broadcast);
});
// Here broadcast is likely not to have a value yet because mysql.query is probably executed in async.
console.log(broadcast.broadcast);
if(err)to see if an error exist which can make the program skip those linesconsole.log()insideif(!err)to see if the lines run, if they do try logging therowand see what's stored inside.add = function(id, cb) { ... ... ... cb(err, row); }Then when you run add do the following:broadcast = new Broadcast(); broadcast.add(5,function(err, row) { console.log(row);});