所以这(我认为)很丑陋。但是我有 的解决方案并且它有效! 是
我创建了一个存储过程(在 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 //
顺便说一句,我确实根据需要复制并更改了代码 - 我希望我记得该网站...
然后在我的快递服务器上我写了一个 knex.raw(...) 语句。而在
.then(result => { ... })
函数(因为 knex 是一个承诺)我通过调用 getError(result) 函数检查了结果是否存在错误。如果它有错误,它会给我钥匙,在我的例子中:'用户名'。 (我仍然必须做没有错误的替代方案)即
---控制台输出---没有错误
ERR_str: [[[{"result":"插入成功,行数 = 1"}],{"fieldCount":0,"affectedRows":0,"insertId":0,"info":"" ,"serverStatus":2,"warning .....它继续
---结束输出-------
仅供参考,这是发生错误 1062 时的控制台输出
---控制台输出---1062错误
ERR_str: [[[{"result":"insert failed, error = 23000, message = Duplicate entry 'leerman2' for key 'username'"}],{"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 并按预期使用它。如果有人发现这种方法有任何问题,请随时告诉我。