React.js and MySQL Resulting in node/no-deprecated-api Error - mysql

Trying to build a backend database for my React app, and upon trying to import the relevant MySQL libraries, it throws an error. There are no problems when I run it from the terminal, but the moment I put it in, I receive the following error:
./src/data/node_modules/mysql/node_modules/safe-buffer/index.js
Line 1:1: Definition for rule 'node/no-deprecated-api' was not found node/no-deprecated-api
Have not been able to find any solutions while searching.
My code:
import {mysql} from 'mysql'; // Commenting out this one line stops the error
export default function tester() {
/*
const connection = mysql.createConnection({ // Commented out only when import is commented
host: 'localhost',
user: '<username>',
password: '<password>',
database: '<db name>'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
*/
}

To anyone reading this in the future, the solution was to not connect directly to the MySQL server, instead to use Express.

Related

How can I handle MySQL disconnection on NodeJS?

First of all, I'm a beginner on NodeJS. Well, I'm using a shared hosting to my project and when the database reaches 1 minute of inactivity, NodeJS crashes and disconnects me from MySQL. Since I'm using a shared hosting, I can't edit the idle time on the MySQL config and I'll need to handle it in code.
I'm using module.exports to handle my connection, as shown below. So how can I make an auto-reconnection script to take care of my issue? Thank you.
var mysql = require('mysql');
module.exports =
{
handle: null,
connect: function(call){
this.handle = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test',
timezone: 'utc',
charset : 'utf8'
});
this.handle.connect(function (err) {
if(err) {
console.log("[MySQL] Connection error: " + err.code);
} else {
console.log("[MySQL] Successfully connected");
}
});
}
};
The node mysql module that you are using also has a connection pooling mechanism.
Check out the docs at https://github.com/mysqljs/mysql#pooling-connections
Connection pools will make you task easier. You can then store the connection pool object and use its getConnection method to obtain a connection. Make sure that you release the connection when you are done with it.
If for some reason you cant use connection pooling then you will have to listen for error event on the connection and handle it accordingly. But I strongly recommend that you use connection pool.

using mysql in Electron

Im trying to use mysql in electron but i'm running into this error
TypeError: Invalid data, chunk must be a string or buffer, not object
at Socket.write (net.js:667)
at Protocol.<anonymous> (Connection.js:100)
with this code
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'host address',
user: 'username',
password: 'pw',
database: 'db name',
});
con.connect(err => {
if(err) throw err
})
con.query('select * from BOOKS', (err2, result) => {
if(err2) {
throw err2;
}
console.log(result);
})
if i paste this into a test.js file and run it with node then it runs 100% fine without errors, so im not really sure where im going wrong here
So it turns it my issue had something to do with the fact that I was running Angular on electron. if I put that code inside my Angular code it would error out but if I put it in the index.html it would run fine.
So I just made a little proxy function in the index.html for connecting to the DB and making queries, then i'd jsut call it from inside the Angular app without a problem like window.mysqlQuery(queryString)

Node.Js - no output from mysql connection

Sorry in advance - using nodejs for the first time..
I have installed nodejs and npm manager on linux machine. Through the npm I installed mysql module and now I try to test the mysql connection using the simple code. The problem is - no output is printed to the console, when the mysql related code is run!
source:
var mysql = require("mysql");
console.log('1');
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "xxx",
password: "xxxx",
database: "xxx",
port: 3306
});
console.log('2');
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
console.log('3');
connection.end(function(err) {
console.log('Connection closed');
});
console.log('4');
process.exit();
The output is 1234:
Here are installed modules:
So my question is - why are there no messages coming from mysql connection? I tried to enter incorrect connection details on purpose - no messages were produced.
I also tried running node as sudo and also tried running nodejs index.js instead of node index.js. Oh, and I also tried to install and use nodejs-mysql module instead of mysql. Nothing seems to be working.
You're writing asynchronous code, so you need to wait for those operations to complete before you force kill the process.
What you're essentially saying is "Do this, do this, do this, and get back to me later. Also shut everything down right now."
Remove the process.exit call or move it inside a callback function so it's triggered at the right time:
var connection = mysql.createConnection({
...
});
console.log('2');
connection.connect(function(err){
if(err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
console.log('3');
connection.end(function(err) {
console.log('Connection closed');
console.log('4');
process.exit();
});
});
This aggressive nesting and re-nesting is why things like promises exist. Bluebird is a great library for implementing and using these and is used by database wrappers like Sequelize to keep your code organized.

How to connect mysql Database with Dart?

Does anyone can tell me how to connect to mysql database with Dart? I've been reading and searching for days but can't find any suitable answers. I just learning web programming. Thank you!
You can use SQLJocky to connect to MySQL. Add
dependencies:
sqljocky: 0.0.4
to your pubspec.yaml an run pub install. Now you can connect to MySQL like this
var cnx = new Connection();
cnx.connect(username, password, dbName, port, hostname).then((nothing) {
// Do something with the connection
cnx.query("show tables").then((Results results) {
print("tables");
for (List row in results) {
print(row);
}
});
});
I think for dart 2 mysql1 is a simple choice.
Example:
import 'package:mysql1/mysql1.dart';
Future main() async {
// Open a connection (testdb should already exist)
final connection = await MySqlConnection.connect(new ConnectionSettings(
host: '10.0.2.2',
port: 3306,
user: 'root',
password: '0123456789',
db: 'development',
));
var results = await connection.query('select * from tableName');
for (var row in results) {
print('${row[0]}');
}
// Finally, close the connection
await connection.close();
}
(tested on Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297))
I haven't tried this, but here is one: http://github.com/jamesots/sqljocky
You can try using sqljocky -> http://pub.dartlang.org/packages/sqljocky

nodejs and mysql connection failing?

var mysql = require('mysql');
var client = mysql.createClient({
user: 'myusername',
password: 'mypassword',
});
client.query('USE mydb');
A simple setup like this using node's mysql is returning an ECONNREFUSED error, although the credentials are correct and permissions are updated. I have even used a different node module to successfully connect/query the database using the same credentials, but the syntax of this module is in my opinion far superior.
Anyone have an idea whats causing this to go wrong?
Error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
In mysql.conf, comment skip-networking.
For creating the database you need to use the command CreateConnection and not createClient, make the necessary changes and that should work !
var mysql = require('mysql');
var client = mysql.createConnection({
host: 'localhost',
user: 'myusername',
password: 'mypassword',
});
client.query('USE mydb');
Note that you have not mentioned which database you are accessing !
Source for this solution:: Check NODEJS Github here