node.js - simple server handling routes and giving error nodejs mysql -
node.js - simple server handling routes and giving error nodejs mysql -
i'm trying write simple server using nodejs , have server ship different queries and/or custom headers/responses based on routes. however, in getusers() function error keeps getting nail , printing 'error querying' console instead of printing email rows. know server connected fine, because can homecoming query when utilize db , homecoming query createconneciton using sec example. help spotting error appreciated. thanks.
what i'm trying done:
var http = require('http'); var mysql = require('mysql'); var url = require('url'); var util = require('util'); var db = mysql.createconnection({ host : "*********", user : "*********", password : "*********", port : '****', database : '*********' }); db.connect(function(err) { console.log('connected'); if (err) console.error('error connecting db' + err.stack); }); function getusers() { db.query('select * users', function(err, rows, fields) { if (err) // changed console.error('error querrying'); console.error(err); if (rows) console.log('rows not null'); (var in rows) { console.log(rows[i].email) } }); } var server = http.createserver(function(req, res) { console.log(req.url); if (req.url == '/signup') { console.log("user signing up"); } else if (req.url == '/signin') { console.log("user signing in"); } else if (req.url == '/new') { console.log("user request new game"); getusers(); } //res.writehead(200); //res.end('hello http'); }); server.listen(3000); // changed , commented out db.end();
what work querying db:
var connection = mysql.createconnection({ host : "********", user : "********", password : "********", port : '****', database : '********' }); connection.connect(); var querystring = 'select * users'; connection.query(querystring, function(err, rows, fields) { if (err) throw err; (var in rows) { console.log('users: ', rows[i].email); } }); connection.end();
the code has been updated changes, , problem was closing database. after changing error logs suggested in comments, error received.
{ [error: cannot enqueue query after invoking quit.] code: 'protocol_enqueue_after_quit', fatal: false }
i commented out
db.end()
and queries returned fine.
thanks help.
mysql node.js routes
Comments
Post a Comment