0
我有這樣的NodeJS代碼:如何在使用mysql2/promise時關閉sql連接?
var mysql = require('mysql2/promise');
mysql.createConnection({
host: "localhost",
user: "root",
password: "123123",
database: "mydatabase"
})
.then((connection) => connection.execute("SELECT * FROM mytable"))
.then(([rows, fields]) => {
console.log(rows);
});
當我執行它,數據庫中的行的應用程序打印陣列,並且還在後運行。似乎它發生,因爲連接沒有釋放。如何在console.log
之後發佈它?
我已經試過這樣:
var mysql = require('mysql2/promise');
mysql.createConnection({
host: "localhost",
user: "root",
password: "123123",
database: "mydatabase"
})
.then((connection) => {
connection.execute("SELECT * FROM mytable")
.then(([rows, fields]) => {
console.log(rows);
connection.release();
});
});
,但結果是TypeError: this.connection.release is not a function
。
對不起,我的英文。
任何想法?
我不認爲'release'是正確的函數名稱。 [參考文檔](https://github.com/mysqljs/mysql#terminating-connections)意味着它是'end'或'destroy'。 – tadman