我编写了这段代码,但是当我在mysql中尝试使用存储过程时,我不知道为什么这段代码与mysql db有错误:
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.
});
这是错误:
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
对不起,伙计们,我是nodejs的初学者。我希望社区能帮助我解决这个问题:D
答案 0 :(得分:2)
我通过更改第20行解决了这个问题:var sql = con.query('call newAccount(?,?,?)',[accountTest.email,accountTest.password,accountTest.bit],function(err) ,result){if(err)throw err; console.log(result.insertId);});
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 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.
});