0

I am writing a Node.js application. In my node-modules/abc/static/example.js file, I want to access the mysql records by querying to the database. Is it possible to do so?

When I include mysql like this:

var mysql = require('mysql');

I get an error:

Uncaught ReferenceError: require is not defined

Is it because it is on client side? What are the other options to access mysql database from the 'example.js' file?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • why are u inside of node_modules? And you can require like this only in server side. – Poliane Brito Nov 25 '15 at 17:05
  • Are you using the MySQL package? https://www.npmjs.com/package/mysql – coreyg Nov 25 '15 at 17:05
  • You cannot access the database directly from the client side. You should send an AJAX request to the server, and have the server access it for you, and return the results. – Madara's Ghost Nov 25 '15 at 17:10
  • "Is it because it is on client side?" — Node JS *isn't* on the client side (at least not in typical use cases where you also have a regular web browser). Node JS code has to run under Node JS not under Chrome/Firefox/Edge/etc – Quentin Nov 25 '15 at 17:14

2 Answers2

1

I'm going to guess that you're attempting to use server side js in the client side.

You need to npm install a mysql adapter, @coreyg suggested this one (https://www.npmjs.com/package/mysql).

Set up a nodejs project (if you have not already done so):

npm init
npm install --save mysql

You would then typically create some sort of webapp that would serve up a url in the client side. You'd then (using ajax) request the url.

Behind the scenes your webapp then would query the mysql database (using the adaptor you installed) with whatever query you had in mind.

Once your ajax call had finished you'd run your callback which would do what you wanted with the data that had been returned from the database.

Thats the basic theory. If you want further information on how to do this I'd suggest reading this question: How do I get started with Node.js

Community
  • 1
  • 1
NMunro
  • 890
  • 5
  • 20
0

Following on Neil Munro's Answer, after initiating nodejs and installing mysql npm package, you can actually execute your code from the terminal. to do that,

you can create a new js file on the same folder you are executing those commands, say mysqlTestProject.js

inside this file, you can put what you had previously, (require stuff and all). for example:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

`` codes were taken from https://www.npmjs.com/package/mysql

and then from the terminal (still on the same folder as you were), type in this line

node ./mysqlTestProject.js
r4ccoon
  • 3,056
  • 4
  • 26
  • 32