var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
in my c# I have
IRestResponse response = client.Execute(request);
var content = response.Content;
but the content prints "Internal Error" for all err types
EDIT
Also tried:
pool.getConnection((err, conn) => {
if(err) {
var error = new Error("something is wrong");
callback(error);
}
according to this https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html, still getting Internal Error for all error types
Also, I do get 200 OK on good requests, what I am trying to do is to print an explanatory message to the client in case of an error
Related
I am creating a facebook chatbot using node js and to store data I used MySQL Database. Currently it is working fine. But, I have a question that, should I need to close the database connection?
I tried to do it but when I close the connection then in the next attempt it is throwing an error that No SQL Connection found
Please tell me the correct way of how to close database connection and reuse it
let databaseConnection = () =>{
let conn = mysql.createConnection({
host: "",
user: "",
password: "",
database: "",
port: ""
});
conn.connect(function (error) {
if (error) {
console.log("Error when connecting to db ",error);
setTimeout(databaseConnection, 2000);
}
else {
console.log("Connected");
}
});
conn.on('error',function(err){
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
databaseConnection();
}else{
throw err;
}
});
return conn;
}
conn = databaseConnection();
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
conn.end()
}
You should end your connection after the loop
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
}
conn.end()
i got the above error
my code was like this
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
con.end();
i tried to do as they explain in the below link but i did not know how to find my socket path?
enter link description here
I'm using RethinkDB with Rethinkdbdash (node.js) and for a few days now Im getting this error:
Unhandled rejection (<[{"entries":3,"id":1357186,"item":{"co...>, no stack trace)
Doesn't matter what query do I run, when i try to get any info from the database I always get the same error. If nothing is returned, error looks like this:
Unhandled rejection (<(empty array)>, no stack trace)
This is my current code:
r.table('example').run().then(function(err, result){
if(err) throw err;
console.log(result);
})
You would need to include the database: 'databasename' field in the config object and pass the config object into run, otherwise you could try to specify the db in your ReQL query.
Specify db for connection:
let connection = null
r.connect( { host: 'localhost', port: 28015, user: 'user', password: 'password', database: 'databasename'}, function(err, conn) {
if (err) throw err
else {
connection = conn
r.table('example').run(connection, function(err, result){
if(err) throw err;
else console.log(`${JSON.stringify(result)}`);
})
}
})
Specify db in query:
let connection = null
r.connect( { host: 'localhost', port: 28015, user: 'user', password: 'password'}, function(err, conn) {
if (err) throw err
else {
connection = conn
r.db('databasename').table('example').run(connection, function(err, result){
if(err) throw err;
else console.log(`${JSON.stringify(result)}`);
})
}
})
Im trying to keep a record from all my errors in a WebService I'm making in node-js;
I've written the following code to keep track of a mysql query possible error:
var err_db = [];
try{
if(error.length == 0){
...
var con = mysql.createConnection({
host: "my_host",
user: "my_user",
password: "my_pass",
database: "my_db"
});
con.connect(function(err) {
if (err) err_db[err_db.length] = err.message;
con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
if (err) err_db[err_db.length] = err.message;
console.log(err_db); //FIRST LOG SHOWS CORRECT
});
console.log(err_db); // THE ERROR DISAPEARS FROM ARRAY
});
}
}
catch(err){
if(err) err_db[err_db.length] = err.message;
}
The problem is the error only keeps stored in array inside the con.query function, after that it disappear, and I want to keep it in a array because later on I intend in sending this possible errors as a JSON to through the WebService response. Thanks in advance.
This is a normal asynchronous nature of node.js. Since the query is executed in a slight greater time so next line is executed first.
try{
if(error.length == 0){
...
var con = mysql.createConnection({
host: "my_host",
user: "my_user",
password: "my_pass",
database: "my_db"
});
con.connect(function(err) {
if (err) err_db[err_db.length] = err.message;
con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
if (err) err_db[err_db.length] = err.message;
console.log(err_db); //FIRST LOG SHOWS CORRECT
// throw the error from here
});
console.log(err_db); // THIS EXECUTED EARLIER THAN THE PREVIOUS
});
}
}
catch(err){
if(err) err_db[err_db.length] = err.message;
}
Asynchronous code cannot catch exceptions using try-catch.
You can try the following code.
var EventEmitter = require('events');
var emitter = new EventEmitter();
var err_db = [];
var con = mysql.createConnection({
host: "my_host",
user: "my_user",
password: "my_pass",
database: "my_db"
});
if (error.length == 0) {
con.connect(function (err) {
if (err) {
emitter.emit('err_db', err);
return;
}
con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
// if (err) err_db[err_db.length] = err.message;
// console.log(err_db); //FIRST LOG SHOWS CORRECT
if (err) {
emitter.emit('err_db', err);
return;
}
});
console.log(err_db); // THE ERROR DISAPEARS FROM ARRAY
});
}
emitter.on('err_db', (err) => {
// handle db err...
err_db[err_db.length] = err.message
});
I have question. How to connect dbconnection.js and demo_api_select.js I have a problem when declare variable on demo_api_select.js. It doesn't work and have error notification like this:
Error Notification
Please help
dbconnection.js:
var mysql=require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
module.exports = con;
demo_api_select.js
var db = require('./dbconnection');
db.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
I want to make those code work as well as this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Try this code.No need to connect twice to database
var db = require('./dbconnection');
db.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});