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)}`);
})
}
})
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
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
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);
});
});
so I'm currently using the following code to execute queries;
var mysql = require('mysql');
var config = require('./config');
var pool = mysql.createPool(config.mysql);
function query(statement){
return new Promise(function(resolve, reject){
pool.getConnection(function(err, connection) {
if(err) reject(err);
connection.query(statement, function(err, row){
connection.release();
if(err){
reject(err);
}
resolve(row);
})
});
});
}
module.exports = {
pool: pool,
query: query
};
Whenever the query function is called it results in a undefined error;
TypeError: Cannot read property 'query' of undefined
I'm quite out of ideas why this could be, this would be connection.getConnection is not returning a proper connection, would this mean my credentials are wrong in my createPool function?
config.mysql
mysql: {
host: 'localhost',
user: 'root',
password: '',
database: 'site',
connectionLimit : 10,
multipleStatements : true
}
The problem is on the following line you are passing in the query function when you should be passing in a string.
connection.query(query, function(err, row){
query in that case should be a string. But you defined query as a function (function query(){)
If you change the line to something LIKE the following this should work.
connection.query("SELECT * from Users", function(err, row){