简体   繁体   中英

Node.js and MySQL function usage

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?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM