Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从mysql/knexjs中提取重复输入错误(1062)的密钥_Javascript_Mysql_Angularjs_Json_Knex.js - Fatal编程技术网

Javascript 如何从mysql/knexjs中提取重复输入错误(1062)的密钥

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.

我通过从html表单发送值来测试我的数据库。独特的插件很好,连接也很好。我的问题是,当我出于测试目的故意发送一个重复的(用户名)时,我会收到一条错误消息。太完美了

然而,我想找出原因,这恰好是关键。错误是根据typeof(error)定义的对象。具体而言:

错误:在
用户
密码
角色
用户名
)值('11121312344','basic','Cthulu')中插入值('1112131234434','basic','Cthulu')-键“username'的重复输入'Cthulu

  • 在使用json.stringify将对象转换为json后,我尝试提取密钥。但唯一可以节省的价值是: {“code”:“ER#u DUP_ENTRY”,“errno”:1062,“sqlState”:“#23000”} 消息的其余部分怎么了?我不能将整个错误转换为字符串吗?然后执行子字符串搜索键?还是我走错了方向
  • 二,。 此外,我还编写了一个函数来遍历对象的键,但是这个对象似乎是一个字符串,但它不是。我似乎无法提取字符串,特别是字符串的这一部分:“'key'username'”

    明确地说,我的目标是将密钥“username”发送回客户机表单,以表明他的用户名已被使用。我以后也会对电子邮件做同样的处理,因此我想要钥匙

    编辑:我相信我必须使用带有GET DIAGNOSTICS的存储过程。类似于。

    所以这(我认为)很难看。但我有一个解决方案,它是有效的!是 我创建了一个存储过程(在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,并按预期使用它。如果有人认为这种方法有任何错误,请随时告诉我。

    所以这(我认为)是丑陋的。但我拥有的解决方案和它的工作原理是 我创建了一个存储过程(在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,并按预期使用它。如果有人认为这种方法有任何错误,请随时告诉我