Reputation: 16764
New to node.js and just cant figure out how to do the following:
I have this on my db module:
var mysql = require('mysql');
var MY_DATABASE='nodejs_mysql';
var client = mysql.createClient({
user: 'root',
password: 'root',
});
and im building this table:
client.query(
'CREATE TEMPORARY TABLE '+USER+
'(username VARCHAR(255), '+
'password VARCHAR(255), '+
'name VARCHAR(255), '+
'picture VARCHAR(255), '+
'PRIMARY KEY(username))'
);
and later on, i want to perform this:
client.query('select username, password from ' + USER + 'where username=?',[req.body.username] , 'AND password=?', [req.body.password]
function (err, results, fields) {
if (err) {
throw err;
}
//some actions performed here
});
all of those are in the same dataBase.js file.
how can i send username and password from another file named: server.js
as parameters to the query written above and get a certain value back?
is there any way to do that?
Upvotes: 0
Views: 1835
Reputation: 101
Why are you splitting your query up?
// server.js
exports.username = "Your username.";
exports.password = "Your password.";
// dataBase.js
// ... mysql connection setup ...
var server = require("./server");
client.query('SELECT username, password FROM `' + USER + '` WHERE username=? AND password=?',
[server.username, server.password],
function (err, results, fields) {
if (err) {
throw err;
}
// ... some actions performed here ...
}
);
I'm not exactly sure what you were asking, but I think this should at least get you closer to your answer.
Upvotes: 0
Reputation: 2228
Ok, I think I get it now. You create temporary table in dataBase.js
and want to perform query in this table in request handler in server.js
. If that's it, you should consider following aproach:
// dataBase.js
var mysql = require('mysql');
var MY_DATABASE='nodejs_mysql';
// connect to database
var client = mysql.createClient({
user: 'root',
password: 'root',
});
// create temporary table
client.query(
'CREATE TEMPORARY TABLE '+USER+
'(username VARCHAR(255), '+
'password VARCHAR(255), '+
'name VARCHAR(255), '+
'picture VARCHAR(255), '+
'PRIMARY KEY(username))'
);
// that's what I think you need. And yes, it looks like "a good to rap" for me.
module.exports.getInfoFromTemporaryTable : function( username, password, callback) {
client.query(
'select username, password from ' + USER + 'where username=?',
[req.body.username] , 'AND password=?', [req.body.password],
callback
);
}
The only thing I can't figure out is where you get USER
variable from. Pay attention to this moment. Maybe pass it to getInfoFromTemporaryTable()
function.
// server.js
var db = require('./lib/dataBase');
app.get(someRoute, function(req, res) {
db.getInfoFromTemporaryTable( req.body.username, req.body.password,
function(err, results, fields) {
if (err) {
// handle errors or something
return;
}
// do what you need to do, using results you got from temp table
});
});
I'm not familiar with MySQL module you using, so above code is more like a general idea of what you need to implement. Hope it will help.
Upvotes: 2
Reputation: 311496
how can i send username and password from another file named: server.js
as parameters to the query written above and get a certain value back?
Use the exports
object. See here for more information on NodeJS's module system.
// server.js
exports.username = function {
return "foo";
};
exports.password = function {
return "bar";
};
// database.js
require('./server.js');
var client = mysql.createClient({
user: server.username, // exports.username in server.js
password: server.password, // exports.password in server.js
});
Upvotes: 0