0

I have just started coding Node. I have a function that name is "add" and there is a mysql query inside the function.

I can't return "row" variable which is inside of the query function.

How can i do this?

Broadcast.prototype.add = function (id) {
    var broadcast;
    mysql.query("SELECT * from Table WHERE Id='" + id + "'", function(err, row) {
        if(!err) {
            return row; //it didn't work
            broadcast = row; //it didn't work
        }
    });
};
6
  • put if(err) to see if an error exist which can make the program skip those lines Commented Aug 22, 2017 at 1:28
  • i tried but there is no error Commented Aug 22, 2017 at 1:29
  • try to put console.log() inside if(!err) to see if the lines run, if they do try logging the row and see what's stored inside Commented Aug 22, 2017 at 1:31
  • Issn't this called Async? You should add a callback function .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);}); Commented Aug 22, 2017 at 1:32
  • Broadcast.prototype.add = function (id) { var broadcast; mysql.query("SELECT * from Stream WHERE Id='" + id + "'", function(err, row) { if(!err) { console.log('there is no error'); return row; //it didn't work broadcast = row; //it didn't work } else { console.log(err); } }); }; Output : there is no error Commented Aug 22, 2017 at 1:35

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Actually, I want to add this row in "Broadcast.prototype.broadcasts" array
after first code and in add function, i wrote "console.log(self.broadcast)". Output : []
Ok so the problem is not the code but the fact that you are not getting any results from mysql. Do you have another program that can connect to your database? Run the exact same query in that program ("SELECT * from Table WHERE Id=[[your id]]) and see if you get any results back. Otherwise try ("SELECT * FROM Table") so you get all the results. Just to see if you get anything.. Are you also sure it is Id and not id and also Tabel and not table?
SELECT * from Table WHERE Id='" + id + "' query returned a row
in the other program or in node?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.