I have just started coding Node. I have a function that name is "add" and there is a mysql query inside the function.
我刚刚开始编码Node。我有一个名为“add”的函数,函数内部有一个mysql查询。
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
}
});
};
1 个解决方案
#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.
return语句(broadcast = row;)之后的代码不会被激活。如果要将行的值分配给广播,则应使它们切换位置。但是在您的评论中,您已经写过您希望将结果添加到阵列广播中。这就是为什么在提供的awnser中你会发现它是一个数组并且行值被添加到它。
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);
#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.
return语句(broadcast = row;)之后的代码不会被激活。如果要将行的值分配给广播,则应使它们切换位置。但是在您的评论中,您已经写过您希望将结果添加到阵列广播中。这就是为什么在提供的awnser中你会发现它是一个数组并且行值被添加到它。
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);