node.js MySQL程序包只应在表中插入多个值时才将其插入表中。
我正在使用node.js MySQL包通过JavaScript插入值。每次我运行查询时,都会将多个值插入到表中,就像查询已多次运行一样。我相信这可能与JavaScript和Promise的异步特性有关。
export function getData() {
return new Promise(function (resolve, reject) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'ec2-xxxxxxxxe-1.amazonaws.com',
user: 'xxxxx',
password: 'xxxxx',
database: 'xxxxoli_webform'
});
connection.connect(function (err) {
var sql = "INSERT INTO webform (Name) VALUES ('test1')";
connection.query(sql, function (err, result1) {
if (result1 === undefined) {
reject(new Error("Error result is undefined"));
} else {
resolve(result1)
}
});
connection.end();
});
});
}
我希望将test1仅输入到表中一次,但是它被插入了4次?