gpt4 book ai didi

node.js - 错误 : No event 'socketConnect' in state 'SentPrelogin'

转载 作者:行者123 更新时间:2023-12-05 01:31:05 26 4
gpt4 key购买 nike

我正在尝试使用 knex 将我的 SQL Server 数据库与 node.js 连接,但我遇到了问题

Error: No event 'socketConnect' in state 'SentPrelogin'
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:26)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
at C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1145:12
at Socket.onConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connector.js:106:7)
at Socket.emit (events.js:314:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
Emitted 'error' event on Connection instance at:
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:12)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
[... lines matching original stack trace ...]
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
[nodemon] app crashed - waiting for file changes before starting...

我的代码是

var knex = require('knex')({  
client: 'mssql',
version:"7_1",
connection: {
user: 'sa',
password: 'Admin@123',
server: 'localhost',
database: 'Demo'
}
});

knex.select("*").from("Country")
.then(function (depts){
depts.forEach((dept)=>{ //use of Arrow Function
console.log({...dept});
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});

最佳答案

你需要添加一个缺失的依赖:

npm install --save tedious

从 knex v0.95.0 开始,当连接到 MSSQL 数据库时,您需要使用 tedious 库而不是 mssql。根据knex upgrade instructions :

MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses tedious library directly instead of mssql, please replace mssql with tedious in your dependencies if you are using a MSSQL database.

安装上面的包应该可以解决您的问题。在连接到我的本地数据库时,我还必须将 encrypt 选项设置为 false 以避免此错误:

ConnectionError: Failed to connect to localhost:1433 - self signed certificate

关于node.js - 错误 : No event 'socketConnect' in state 'SentPrelogin' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66504525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com