我正在调用两个函数,其中第一个函数是在表中插入数据。我正在使用mysql
。第二个是计算它的值并显示它。
问题是,第二个功能是在插入表之前进行处理。我做错了什么?。
我的代码是:
function_my(id);
console.log('id inserted');
count(id);
function_my(id){
var mysql = require('mysql2');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'gcm',
});
for(var id=0; j < 10; j++){
connection.query('INSERT INTO UserTable SET ?', {id : id}, function(err, res){
if(err) throw err;
});
}
count(id){
connection.execute('select id from UserTable where id = ?', id , function(err, rows){
console.log('Test :'+JSON.stringify(rows[0].id));
});
}
输出是:
Test : undefined
Id inserted
为什么我要面对这个问题?
答案 0 :(得分:1)
您应该在count
操作后调用insert
函数。
在插入所有记录之后,count(id)
应该用作回调函数。
您可能需要查看一些Promise
库(例如bluebird
)以获取更多信息。