Search code examples
mysqlnode.jsstored-proceduresnode-mysql

stored-procedure return results that I do not want


I use node-mysql to call a stored-procedure, result contains some schema I dont want like fieldCount,affectedRows,how to ignore these?

when I use mysql commend to call the procedure there is no these schemas.

procedure:

DELIMITER \\
CREATE PROCEDURE QUERY_USER (IN p_name VARCHAR(15))
BEGIN
    SELECT id,name FROM user WHERE name=p_name;
END\\

Result:

[   
    [
    {
      "id": 2,
      "name": "codar"
    },
    {
      "id": 3,
      "name": "codar"
    }   
    ],   
    {
    "fieldCount": 0,
    "affectedRows": 0,
    "insertId": 0,
    "serverStatus": 34,
    "warningCount": 0,
    "message": "",
    "protocol41": true,
    "changedRows": 0   
    } 
]

Solution

  • The results you are looking for are not members of the array that you are looking at... they are nested in the first element of that array, which is itself another array, which in your example only has one member, but if it had more rows, they would be members of the inner array, not the outer one. You need to move one level in.

    With stored procedures, you expect to find the first row of the first result-set at element [0][0], the second row (if there is one) at [0][1], the third (if there is one) at [0][2]... and so on. The results are not in [0] then [1] then [2], they're all in an array nested inside [0].