MYSQL Node Issue

问题: Running the below code throws a MYSQL err, I've checked the syntax and it seems correct but not sure why the error is being thrown. app.post('/:roomname/create',function(...

问题:

Running the below code throws a MYSQL err, I've checked the syntax and it seems correct but not sure why the error is being thrown.

app.post('/:roomname/create',function(request, response){
let roomID =  ""+request.params.roomname+"";
console.log(roomID);
let insertRoom = "INSERT INTO chatrooms (roomname) VALUES (?)";


conn.query(insertRoom,[roomID], function(error, result) {
  if (err) throw err;
console.log("1 record inserted");
});

Error:

node_modules/mysql/lib/protocol/Parser.js:80
    throw err; // Rethrow non-MySQL errors
    ^

ReferenceError: err is not defined


回答1:

Your handling error name and callback error name is not same! you must define same error name in both callback and handling block. Please check use this snipet. After use this snipet if any error happening, please let me know in comment.

app.post('/:roomname/create',function(request, response){
let roomID =  ""+request.params.roomname+"";
console.log(roomID);
let insertRoom = "INSERT INTO chatrooms (roomname) VALUES (?)";


conn.query(insertRoom,[roomID], function(err, result) {
  if (err) throw err;
console.log("1 record inserted");
});

回答2:

conn.query(insertRoom,[roomID], function(error, result) {
  if (err) throw err;

In the callback function you are writing "error" but in the if you are checking "err". In the callback change it to

conn.query(insertRoom,[roomID], function(err, result) {
  • 发表于 2019-03-19 06:26
  • 阅读 ( 457 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除