我正在学习node.js.编写此代码以从DB获取结果并尝试将其添加到全局数组中。使用array.push或array [index] = object添加结果,但是在connection.query范围之外无法访问它。 这是我的剧本。
var userData = [];
var query = 'SELECT * FROM user_registration limit 5';
connection.query(query, function(err, rows) {
if (err) throw err;
for (var i in rows) {
userData[i]=[];
userData[i]=rows[i];
//console.log(userData);
}
//console.log(userData);
});
console.log("Printing Query result");
console.log(userData);
var name='';
var numArr=[];
var query = 'SELECT * FROM user_registration limit 5';
connection.query(query, function(err, rows) {
for(var j = 0; j < 5;j++){
numArr[j]=[];
numArr[j]=j;
}
name="Tasleem";
});
console.log("Printing Simple Array");
console.log(numArr);
console.log("Name :"+name);