Robson Posted September 11, 2018 Share Posted September 11, 2018 (edited) 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. this is my work folder Edited September 11, 2018 by Robson Link to comment Share on other sites More sharing options...
MrPancakers Posted September 11, 2018 Share Posted September 11, 2018 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 Link to comment Share on other sites More sharing options...
Hoser Posted September 11, 2018 Share Posted September 11, 2018 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. Link to comment Share on other sites More sharing options...
Robson Posted September 11, 2018 Author Share Posted September 11, 2018 (edited) 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 September 11, 2018 by Robson Link to comment Share on other sites More sharing options...
Hoser Posted September 11, 2018 Share Posted September 11, 2018 (edited) 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 September 11, 2018 by Hoser Link to comment Share on other sites More sharing options...
Robson Posted September 11, 2018 Author Share Posted September 11, 2018 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 Link to comment Share on other sites More sharing options...
Hoser Posted September 11, 2018 Share Posted September 11, 2018 (edited) 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 September 11, 2018 by Hoser 1 Link to comment Share on other sites More sharing options...
Robson Posted September 11, 2018 Author Share Posted September 11, 2018 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 Link to comment Share on other sites More sharing options...
MrPancakers Posted September 11, 2018 Share Posted September 11, 2018 (edited) 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 September 11, 2018 by MrPancakers Link to comment Share on other sites More sharing options...
MrPancakers Posted September 11, 2018 Share Posted September 11, 2018 1 hour ago, Robson said: Alright this is what I get now what happens if you type the details manually? I copy pasted your code and Step 3 prints for me. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now