我正在尝试将这个solution实现到我的代码中。但是,我在尝试调用回调函数中的函数时遇到了麻烦。
在我的index.js
文件中,我有这样一个
const db = require('./models/db.js');
db.connectDb();
在我的db.js
文件中,我有以下内容:
const mysql = require('mysql');
const database = {
//Connects to the DB
connectDb : function () {
// Create connection
this.db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'example'
});
this.db.connect(function(err){
if(err){
console.log("An error has occured when trying to connect to db.");
throw err;
}
console.log("MySQL Connected...");
});
this.db.on('error', function (err) {
var currentdate = new Date();
consoleoccurredn error has occured @ ', currentdate,' with error:', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
throw err;
}
});
},
handleDisconnect: function () {
this.connectDb();
}
}
module.exports = database;
服务器启动,MySQL连接成功创建,由于我的目的是处理连接丢失的错误,所以我重新启动了服务器,正如预期的那样,this.db.on('error', function (err)
被执行,如终端所示。
MySQL Connected...
An error has occurred @ [datetime] with error: Error: Connection lost: The server closed the connection.
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
但是,我得到这个错误:
/home/folder/models/db.js:27
this.handleDisconnect();
^
TypeError: this.handleDisconnect is not a function
at Connection.<anonymous> (/home/folder/models/db.js:27:22)
at Connection.emit (node:events:526:28)
at Connection._handleProtocolError (/home/folder/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (node:events:526:28)
at Protocol._delegateError (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
[nodemon] app crashed - waiting for file changes before starting...
**我的问题:**这个错误是否与试图调用回调函数之外的函数有关?是否有可能调用回调函数之外的函数,而该回调函数在对象的函数内部?或者问题是别的什么?
以下是终端中的输出,供您参考。
MySQL Connected...
An error has occurred @ [datetime] with error: Error: Connection lost: The server closed the connection.
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
/home/folder/models/db.js:27
this.handleDisconnect();
^
TypeError: this.handleDisconnect is not a function
at Connection.<anonymous> (/home/folder/models/db.js:27:22)
at Connection.emit (node:events:526:28)
at Connection._handleProtocolError (/home/folder/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (node:events:526:28)
at Protocol._delegateError (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
[nodemon] app crashed - waiting for file changes before starting...
1条答案
按热度按时间guicsvcw1#
在此代码中调用
this.handleDisconnect()
时出现的问题:this
的值不是你想要的值这就是为什么你会得到this.handleDisconnect
不是函数的错误(可能是undefined
)。普通函数回调将根据调用者如何设置自己的this
值(即您的数据库)调用它。相反,你可以把它改成一个箭头函数,它会保留所需的this
值,这就是发明箭头函数的主要原因(保留this
的词法值):这里的另一个问题是
throw err
在这个上下文中将没有用处。你将把它扔回到你数据库中的某个异步上下文中,而那不是你可以捕捉或处理那个错误或对它做任何有用的事情的地方。所以,你将需要一个更有用的方法来处理那个错误。