0

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?

2
  • Not quiet sure what you exactly wanted to do, but at first glance it looks like you need to read about node's modules and maybe module.exports (nodejs.org/docs/latest/api/modules.html#module.exports) Commented Jan 22, 2012 at 18:02
  • what i meant is to ask is it ok to "rap" the last part of code in a function which receives params and returns a value? Commented Jan 22, 2012 at 18:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! about the USER variable, its not a variable its a table which is not written here...:) thanks again!
0

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
});

3 Comments

i think i was misunderstood here... i want to get values to the query: client.query('select username, password from ' + USER + 'where username=?',[req.body.username] , 'AND password=?', [req.body.password] and return some value... i think that your answer will not do that. i need to add some more queries and let them return some more values... is it clearer now?
You want to get values for query from where exactly? Other module? Other DB query? As far as I understand, the source is temporary table created in dataBase.js file. When request comes you want query some values from this temporary table and use them in request handler. Am I right?
@elmigranto i want to get values for query from other module,yes. is it good to "rap" the last query in a function which receives params and returns some things?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.