Javascript 如何从mysql/knexjs中提取重复输入错误(1062)的密钥
我通过从html表单发送值来测试我的数据库。独特的插件很好,连接也很好。我的问题是,当我出于测试目的故意发送一个重复的(用户名)时,我会收到一条错误消息。太完美了 然而,我想找出原因,这恰好是关键。错误是根据typeof(error)定义的对象。具体而言: 错误:在Javascript 如何从mysql/knexjs中提取重复输入错误(1062)的密钥,javascript,mysql,angularjs,json,knex.js,Javascript,Mysql,Angularjs,Json,Knex.js,我通过从html表单发送值来测试我的数据库。独特的插件很好,连接也很好。我的问题是,当我出于测试目的故意发送一个重复的(用户名)时,我会收到一条错误消息。太完美了 然而,我想找出原因,这恰好是关键。错误是根据typeof(error)定义的对象。具体而言: 错误:在用户(密码,角色,用户名)值('11121312344','basic','Cthulu')中插入值('1112131234434','basic','Cthulu')-键“username'的重复输入'Cthulu 在使用json.
用户
(密码
,角色
,用户名
)值('11121312344','basic','Cthulu')中插入值('1112131234434','basic','Cthulu')-键“username'的重复输入'Cthulu
DELIMITER //
DROP PROCEDURE IF EXISTS `insert_new_user` //
CREATE PROCEDURE `insert_new_user` (uname VARCHAR(45), pass CHAR(96), r varchar(15))
BEGIN
/**
Return field that has triggered the error, otherwise, return good
**/
-- Declare variables to hold diagnostics area information
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
DECLARE rows INT;
DECLARE result TEXT;
-- Declare exception handler for failed insert
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
END;
-- Perform the insert
INSERT INTO Users(username, password, role) VALUES (uname, pass, 'full');
-- Check whether the insert was successful
IF code = '00000' THEN
GET DIAGNOSTICS rows = ROW_COUNT;
SET result = CONCAT('insert succeeded, row count = ',rows);
ELSE
SET result = CONCAT('insert failed, error = ',code,', message = ',msg);
END IF;
-- Say what happened
SELECT result;
END //
顺便说一句,我确实根据需要复制和修改了代码-我希望我记得这个网站
然后在我的express服务器中,我写了一个knex.raw(…)语句。在
.then(result => { ... })
函数(因为knex是一个承诺),我通过调用getError(result)函数来检查结果是否有错误。如果它有错误,它会给我密钥,在我的例子中是:“username”。(我仍然要做的选择没有错误)即
---控制台输出---无错误
ERR_str:[[{“结果”:“插入成功,行计数=1”}],{“字段计数”:0,“受影响的行”:0,“插入ID”:0,“信息”:“服务器状态”:2,“警告…”继续
---终端输出-------
仅供参考,这里是发生错误1062时的控制台输出
---控制台输出---用于1062错误
ERR_str:[[[{“result”:“insert failed,error=23000,message=key'username'}的重复条目'leerman2',{“fieldCount”:0,“affectedRows”:0,“insertId”:0,“info”:“,“serverStatus”:2,“warningStatus”:0},[{b…它继续
---终端输出-------
下面是密钥提取的代码:
function getError(err) {
const ERR_str = JSON.stringify(err);
console.log(`Entered getError() function...proceeding to exctract error...`);
console.log(`ERR_str: ${ERR_str}`);
console.log(`Error is string. [typeOf(ERR_str)]: ${typeof(ERR_str)}. (Attempting to exctract key...`);
const START = (ERR_str.indexOf('key')) + 'key'.length + 2; // 2 including spaces
console.log(`Start: ${START}`);
const END = ERR_str.indexOf(`'"}`);
console.log(`End: ${END}`);
const KEY = ERR_str.substr(START, END - START);
console.log(`key to return to server: ${KEY}`);
return KEY || 0;
}
不是最干净的代码,但我认为这是一个好的开始。我现在可以将它发送到Angular,并按预期使用它。如果有人认为这种方法有任何错误,请随时告诉我。所以这(我认为)是丑陋的。但我拥有的解决方案和它的工作原理是
我创建了一个存储过程(在mysql中),包括GET DIAGNOSTICS命令
DELIMITER //
DROP PROCEDURE IF EXISTS `insert_new_user` //
CREATE PROCEDURE `insert_new_user` (uname VARCHAR(45), pass CHAR(96), r varchar(15))
BEGIN
/**
Return field that has triggered the error, otherwise, return good
**/
-- Declare variables to hold diagnostics area information
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
DECLARE rows INT;
DECLARE result TEXT;
-- Declare exception handler for failed insert
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
END;
-- Perform the insert
INSERT INTO Users(username, password, role) VALUES (uname, pass, 'full');
-- Check whether the insert was successful
IF code = '00000' THEN
GET DIAGNOSTICS rows = ROW_COUNT;
SET result = CONCAT('insert succeeded, row count = ',rows);
ELSE
SET result = CONCAT('insert failed, error = ',code,', message = ',msg);
END IF;
-- Say what happened
SELECT result;
END //
顺便说一句,我确实根据需要复制和修改了代码-我希望我记得这个网站
然后在我的express服务器中,我写了一个knex.raw(…)语句
.then(result => { ... })
函数(因为knex是一个承诺)我通过调用getError(result)函数来检查结果是否有错误。如果它有错误,它将为我获取密钥,在我的例子中是:“username”。(我仍然需要做其他选择,以确保没有错误),即
---控制台输出---无错误
ERR_str:[[{“结果”:“插入成功,行计数=1”}],{“字段计数”:0,“受影响的行”:0,“插入ID”:0,“信息”:“服务器状态”:2,“警告…”继续
---终端输出-------
仅供参考,这里是发生错误1062时的控制台输出
---控制台输出---用于1062错误
ERR_str:[[[{“result”:“insert failed,error=23000,message=key'username'}的重复条目'leerman2',{“fieldCount”:0,“affectedRows”:0,“insertId”:0,“info”:“,“serverStatus”:2,“warningStatus”:0},[{b…它继续
---终端输出-------
下面是密钥提取的代码:
function getError(err) {
const ERR_str = JSON.stringify(err);
console.log(`Entered getError() function...proceeding to exctract error...`);
console.log(`ERR_str: ${ERR_str}`);
console.log(`Error is string. [typeOf(ERR_str)]: ${typeof(ERR_str)}. (Attempting to exctract key...`);
const START = (ERR_str.indexOf('key')) + 'key'.length + 2; // 2 including spaces
console.log(`Start: ${START}`);
const END = ERR_str.indexOf(`'"}`);
console.log(`End: ${END}`);
const KEY = ERR_str.substr(START, END - START);
console.log(`key to return to server: ${KEY}`);
return KEY || 0;
}
不是最干净的代码,但我认为这是一个好的开始。我现在可以将它发送到Angular,并按预期使用它。如果有人认为这种方法有任何错误,请随时告诉我