Howard
Howard

Reputation: 35

Mysql "INSERT INTO" error throw with Node.js

I use the following code using mysql with Node.js

var mysql = require('mysql');
var db = mysql.createConnection({host:'localhost', user:'root', password: 'pw', database: 'db'});
db.connect();

And, I use this to SELECT data and handle error.

db.query("SELECT * FROM user_info WHERE uid = ? LIMIT 1", [selfid], function(err, rows, fields) {
    if (err) throw err;
    // do something....
});

But, I want to ask that when I use "INSERT" and some error happens, will the app.js crash if I haven't handle error?

var qstr = "INSERT INTO user_info (uid, message, created_time) VALUES(?, ?, CURRENT_TIMESTAMP)";
db.query(qstr, [uid, message]);

Upvotes: 1

Views: 2141

Answers (1)

000
000

Reputation: 27247

No, the app will not crash if you do not handle your errors. It will continue silently. It is up to you to catch the errors and handle them.

var qstr = "INSERT INTO user_info syntax error";
db.query(qstr, function(err) {
    if (err) {
        console.log("A wild error appeared!", err);
        process.exit(1);
    }
});

Upvotes: 1

Related Questions