node.js script not connecting to mysql database - javascript

When i run my node.js script i get this error:
http://i.gyazo.com/4abc4f518db0de3cb36e34a9fa163e22.png
I was reading a similar error and the guy said it was because the script wasn't connecting to the mysql database. I've tried all of the different logins i can think of and it still wont work. I have tried connecting with no credentials and i get the same error. These are the current credentials i am using to log in:
host : 'localhost',
user : 'root',
password : 'mypassword',
database : 'milky',
The code where the error comes from:
mysqlConnection.query('SELECT `value` FROM `info` WHERE `name`=\'maxitems\'', function(err, row, fields) {
if(offer.items_to_receive.length > row[0].value) {
offers.declineOffer({tradeOfferId: offer.tradeofferid});
offer.items_to_receive = [];
mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`) VALUES (\''+offer.steamid_other+'\',\'toomuch\',\'System\')', function(err, row, fields) {});
return;

Related

How to render rows of data in React.js from mysql database using Express.js?

Can someone explain or point to some tutorial where is explained how to render rows of data from mySql database into react.js component?
I've made small database using mysql workbench and this baza.js file inside my project folder:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'db'
});
connection.connect(function(err) {
if (err) throw err;
connection.query("SELECT * FROM promjer", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
When I run node baza.js in my CMD, everything seems fine, I get everything from that specific table inside CMD terminal so I guess my database is ok and it's connected with app.
What troubles me is how to render that data inside my app?
I know that React by itself can't handle data so i should use Express.js. But I don't get what to do with it. Express should be running on other port so how should I even get data to component in app which is running on port 3000 if express is running on port 9000?
Thanks in advance!

Node.js SQL How to Run Query that Spans Over Two Databases

I am building an application that has a backend that uses SQL queries to get data from a SQL Server database. However, I need to write a query that truncates and repopulates a table in that database using data from a second database. Here is what my code looks like:
// establishes a connection to serverName and uses DB1 as the database. But how can you access two?
global.config = {
user: 'username',
password: 'password',
server: 'serverName',
database: 'DB1'
};
// run this query. It's already been tested in SQL server and works fine there
let query = "TRUNCATE TABLE [DB1].[dbo].[Shop]; INSERT INTO [DB1].[dbo].[Shop] (Shop, shopDescription, Address, City)" +
" SELECT Shop, Description, Address, City FROM [DB2].[dbo].[ShopTable]"
new sql.ConnectionPool(config).connect().then(pool => {
return pool.request().query(query) }).then(
result => {
console.log(result.recordset)
//result returns as "undefined"
res.setHeader('Access-Control-Allow-Origin', '*')
res.status(200);
sql.close();
}).catch(err => { //error is not thrown
res.status(500).send({ message: err})
sql.close();
});
I get an "undefined" result, and find that no update to the table was made. The issue here isn't exactly clear whether it can't reach the table in DB2, or if perhaps the command doesn't work with the Node.js mssql package?
It appears this was a front end to back end connection issue. I was able to get the query to work without any changes to the database information, so it appears I can access the second database without any issues.
component.ts
this.httpService.patch('update', {} ).subscribe()
index.js
app.patch('/api/update', controllers.manualQueries.update);

How to connect to remote mysql database using Node.js

I'm trying to connect to a mysql database using node. It is working perfectly fine when connecting with localhost.
var pool = mysql.createPool({
connectionLimit : 100,
host : 'http://<ip-address here>/phpmyadmin',
user : '*********',
password : '*********',
database : '*********'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT * FROM table', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
The above code is not working.
I haven't found any answer on Google as well as here.
Host is only contains IP address, try to remove phpmyadmin.

Node.JS - Check result exists before running function

I'm relatively new to Node JS. I'm attempting to query the database, and then simply emit that data to the front end using socket.io, however I've noticed that it is intermittently sending the data / not sending the data to the front end. I'm assuming that the reason behind this is because the query has not yet finished yet, and was wondering how you'd wait for the result to be accessible before continuing?
I'm using npm mysql to access the database within the socket such as below:
var mysql = require('mysql');
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : '',
database : 'db'
}
);
Here is my query:
connection.query(queryString, function(err, result, fields){
if(err) throw err;
socket.emit('emitFunction', result);
});
The callback only called after query done so the result should available inside the callback function. Log both err and result to see what happened

read sql query output in javascript

I am trying to read the SQL query output in javascript
I am using Node.js , Express Engine (a MVC model) to read the MySQL database and display the output on UI using Jade.
I have a file index.js which fetches value of 'password' and 'user_name'from homepage.jade form field.
I have to check if the password entered by user matches the password in the database.
for which I am querying the database to get a matching record for the entered username and want to fetch the password value for that user and check if it is equal to the form value of password.
router.get('/sample', function(req, res) {
db=mysql.createConnection(config);
db.connect(function(err){
});
var formpass= req.body.password;
var user_name= req.body.userName;
var sql="select * from user where user_name= 'monicathaneer'";
db.query(sql, function(err_query,data)
{
for a in data
pass= a.password;
}
if(formpass==pass)
{
res.render('Login', {
"Login" : data });
}
});
I get errors saying unexpected token near 'a' and unexpected token near 'if' when I run the node.
I would like to know how can I can read the password property from the sql output in a .js file.
Your code sample has some syntax errors and lost curly braces. Try this (assuming you're using node-mysql module):
router.get('/sample', function(req, res) {
var formpass = req.body.password,
user_name = req.body.userName,
sql = "select * from user where user_name = 'monicathaneer'";
var db = mysql.createConnection(config);
db.connect(function (err) {
// you should handle the errors here
});
db.query(sql, function (err_query, data) {
// I'm assuming your query will return just 1 row
// because usernames should be unique
if (formpass === data[0].password) {
res.render(
'Login',
{"Login": data}); // In your context, 'data' contains the user password...
// Do you really want to send it back to the web page?
}
});
db.end(); // you forgot to close your connection.
});
Some considerations:
You should not try to handle authentication manually. Consider using a middleware like Passport.js;
You don't need to open and close your connection on each request. Try to refactor your code in order to open the connection when the app starts, and use it on demand.

Categories