Sign in to follow this  
Robson

mysql node.js begginner

Recommended Posts

Hello my friends.  I'm trying to use mysql with node.js.  This is my code trying to output a response in my  .connect() but I get nothing from console.
 

var db = require("../json/connection.json");

console.log('step 1');

var mysql = require("mysql");
var connection = mysql.createConnection({
	host : db["host"],       
	user : db["user"],
	password : db["password"],
	database : db["database"],
	port : db["port"]
});

console.log('step 2');

connection.connect(function(err){
	console.log('step 3');
});

There's no 'step 3' being outputed right there. It shows no error either.EJwuFv7.png



this is my work folder 

G5Qn0id.png

Edited by Robson

Share this post


Link to post
Share on other sites

Maybe it's not connecting, you aren't doing any error handling so you don't even know if there's an error occuring. If there is an error, your console.log won't work

connection.connect(function(err){
	if(!err){
		console.log('step 3');
	} else {
		console.log(err);
	}
});

Try that

Share this post


Link to post
Share on other sites

Did you confirm the "db" array is actually correct?

Try putting the values in the createConnection directly, or try to print them to the console.

Share this post


Link to post
Share on other sites
40 minutes ago, MrPancakers said:

Maybe it's not connecting, you aren't doing any error handling so you don't even know if there's an error occuring. If there is an error, your console.log won't work

connection.connect(function(err){
	if(!err){
		console.log('step 3');
	} else {
		console.log(err);
	}
});

Try that

Hello mr pancakes already tried it before. Even not handling a error the step 3 would output anyway I think. Maybe theres something that I did miss on nodejs setup?

11 minutes ago, Hoser said:

Did you confirm the "db" array is actually correct?

Try putting the values in the createConnection directly, or try to print them to the console.

Yes hoser, they're accurate. I'm trying to integrate my MTA server database with this (located in amazon). Same values, I already did it but I'll try to do it again (im stuck here for hours)

 

I already printed then with console.log to make sure it was getting it right

Edited by Robson

Share this post


Link to post
Share on other sites
3 minutes ago, Robson said:

Hello mr pancakes already tried it before. Even not handling a error the step 3 would output anyway I think. Maybe theres something that I did miss on nodejs setup?

Oh, you have to actually point to the mysql location, it's in node_modules by default.

 

var mysql = require('../node_modules/mysql');

(This is assuming you're in /packages)

Edited by Hoser

Share this post


Link to post
Share on other sites
2 minutes ago, Hoser said:

Oh, you have to actually point to the mysql location, it's in node_modules.

 

 

var mysql = require('../node_modules/mysql');

(This is assuming you're in /packages)

Tried it now and got the same result

D0TlQx2.png

 

Share this post


Link to post
Share on other sites

Make sure you're in the right directory, right after you require mysql, try:

 

mysql.exists('index.js', function(exists) { 
  if (exists) { 
    console.log("FOUND IT!");
  } 
else {
    console.log("Mysql not found.");
}); 

 

Edited by Hoser
  • Like 1

Share this post


Link to post
Share on other sites
5 minutes ago, Hoser said:

Make sure you're in the right directory, right after you require mysql, try:

 

mysql.exists('index.js', function(exists) { 
  if (exists) { 
    console.log("FOUND IT!");
  } 
else {
    console.log("Mysql not found.");
}); 

 

Alright this is what I get now 

71mPuMm.png

Share this post


Link to post
Share on other sites
1 hour ago, Hoser said:

Oh, you have to actually point to the mysql location, it's in node_modules by default.

 

var mysql = require('../node_modules/mysql');

(This is assuming you're in /packages)

You shouldn't need to write the whole path, writing require('mysql') should work just fine if his code is in packages and his node_module folder is in the root directory. He'd receive an error already if the mysql module wasn't found.

Edited by MrPancakers

Share this post


Link to post
Share on other sites
1 hour ago, Robson said:

Alright this is what I get now 

71mPuMm.png

what happens if you type the details manually? I copy pasted your code and Step 3 prints for me.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.