Node.js NodeJS SQLite错误异常
我目前正在尝试使用NodeJS创建一个服务器,以提供restfulapi将记录插入SQLite数据库。虽然我能够检索记录,但在处理SQLite相关错误(例如,唯一约束)时遇到问题: 问题在于,代码似乎同时命中了第二个和第三个函数,因为每当我尝试在handleError方法中创建响应时,响应对象似乎是关闭的。在我的浏览器中,我收到“User registered.”消息作为响应Node.js NodeJS SQLite错误异常,node.js,sqlite,http,error-handling,Node.js,Sqlite,Http,Error Handling,我目前正在尝试使用NodeJS创建一个服务器,以提供restfulapi将记录插入SQLite数据库。虽然我能够检索记录,但在处理SQLite相关错误(例如,唯一约束)时遇到问题: 问题在于,代码似乎同时命中了第二个和第三个函数,因为每当我尝试在handleError方法中创建响应时,响应对象似乎是关闭的。在我的浏览器中,我收到“User registered.”消息作为响应 SQLITE_CONSTRAINT: UNIQUE constraint failed: Users.name, Use
SQLITE_CONSTRAINT: UNIQUE constraint failed: Users.name, Users.phone_no
events.js:85
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15)
at handleError (/Users/kli/Documents/Node.js/db.js:59:14)
at Statement.<anonymous> (/Users/kli/Documents/Node.js/db.js:39:3)
at Statement.replacement (/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)
at Statement.replacement (/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)
SQLITE\u约束:唯一约束失败:Users.name,Users.phone\u no
events.js:85
投掷者;//未处理的“错误”事件
^
错误:结束后写入
在ServerResponse.OutgoingMessage.write(_http_outgoing.js:413:15)
在handleError(/Users/kli/Documents/Node.js/db.js:59:14)
在声明中。(/Users/kli/Documents/Node.js/db.js:39:3)
at Statement.replacement(/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)
at Statement.replacement(/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)
处理NodeJS中错误的正确方法是什么?我尝试使用process.on('uncaughtException',function(err)方法,但我需要response对象才能向客户端发送响应,指示已发生的错误
SQLITE_CONSTRAINT: UNIQUE constraint failed: Users.name, Users.phone_no
events.js:85
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15)
at handleError (/Users/kli/Documents/Node.js/db.js:59:14)
at Statement.<anonymous> (/Users/kli/Documents/Node.js/db.js:39:3)
at Statement.replacement (/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)
at Statement.replacement (/Users/kli/node_modules/sqlite3/lib/trace.js:20:31)