我在用node查询数据库中使用如下代码:
var _password =mysql.query(table," where name = " + "'" + name + "'","password");
其中mysql.query为
query(table,others,column){
var connection = this.connection;
var sql;
if(column) sql = "select " + column + " from " + table;
else{sql = "select * from " + table;}
if(others) sql += " " + others;
console.log(sql);
var result;
connection.query(sql,function(err,rows,fields){
if(err){
throw err;
}
if(rows.length > 1){
rows.forEach(function(row){
console.log(row);
});
}
else if(rows.length == 1){
console.log(rows[0]);
rows = rows[0];
}
else{
console.log("没有数据");
}
return rows;
});
console.log('result:');
console.log(result);
return result;
}
最终的结果是:
很显然query之后的语句先执行了,此处无法使用yield,请大神指教
The loop here is asynchronous, so if something goes wrong, you need to control the asynchronousness.
Let me give you an example. I just learned to write, so I’m not very good at writing, but I can show you how to control asynchronousness.