(Solved) Insert sử dụng Store Procedure trong Mysql với nodejs ???



  • Em viết code thế này:

    var mysql = require("mysql");

    var con = mysql.createConnection({
    host: "localhost",
    port: "8889",
    user: "root",
    password: "root",
    database: "ResetAccountTest"
    });

    con.connect(function(err){
    if(err){
    console.log('Error connecting to Database');
    return;
    }
    console.log('Connection established');
    });

    var accountTest = { email: 'mra@fpt.edu.vn', password: 'root', bit: '1' };
    con.query('call newAccount(?, ?, ?)', accountTest, function(err, result) {
    if (err) throw err;

    console.log(result.insertId);
    });

    con.end(function(err) {
    // The connection is terminated gracefully
    // Ensures all previously enqueued queries are still
    // before sending a COM_QUIT packet to the MySQL server.
    });

    Nhưng lỗi lại báo ra là :

    Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?)' at line 1
    at Query.Sequence._packetToError (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
    at Query.ErrorPacket (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)
    at Protocol._parsePacket (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/Protocol.js:274:23)
    at Parser.write (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/Parser.js:77:12)
    at Protocol.write (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/Connection.js:96:28)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    --------------------
    at Protocol._enqueue (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Connection.query (/Users/mikegraham/Documents/WebstormProjects/reset-password/node_modules/mysql/lib/Connection.js:201:25)
    at Object.<anonymous> (/Users/mikegraham/Documents/WebstormProjects/reset-password/app.js:20:5)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:136:18)
    at node.js:972:3

    Rất mong các cao nhân giúp đỡ em :(. Em chân thành cảm ơn !



  • var mysql = require("mysql");

    var con = mysql.createConnection({
    host: "localhost",
    port: "8889",
    user: "root",
    password: "root",
    database: "ResetAccountTest"
    });

    con.connect(function(err){
    if(err){
    console.log('Error connecting to Database');
    return;
    }
    console.log('Connection established');
    });

    var accountTest = { email: 'mra@fpt.edu.vn', password: 'root', bit: '1' };
    var sql = con.query('call newAccount(?, ?, ?)', [accountTest.email, accountTest.password, accountTest.bit], function(err, result) {
    if (err) throw err;

    console.log(result.insertId);
    });

    con.end(function(err) {
    // The connection is terminated gracefully
    // Ensures all previously enqueued queries are still
    // before sending a COM_QUIT packet to the MySQL server.
    });


Log in to reply