Need help with module.exports!!!

104 views
Skip to first unread message

Bob Spero

unread,
Mar 6, 2014, 4:50:59 PM3/6/14
to nod...@googlegroups.com
I am using node oracle to connection.execute("SELECT alot of stuff FROM inventory"). So I took all my original sql files and I put them in a folder. Now I want to use node to read the file contents and do  connection.execute(function(inventory.sql)) where in the sql file is my select. Some of the queries have three table joins so it make the code look messy and it just seems more organize. Below is what I have so far but I just can not get it working! Thanks


fs = require('fs');
var select_statement = "";
var sql_file = function(value) {
    fs.readFile('./SQLs/inventory.sql', 'utf8', function(err, data) {
        if (err) {
            return console.log(err);
        };
        return data.toString();
    });
    //return value //+ select_statement;
};
module.exports.select_statement = select_statement;
module.exports.sql_file = sql_file;

Colin Teal

unread,
Mar 6, 2014, 4:57:00 PM3/6/14
to nod...@googlegroups.com
It looks like you need to use readFileSync rather than readFile (which is asynchronous). Otherwise you won't be able to return the contents of your file in the manner in which you've described (synchronous). 

Colin


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rick Waldron

unread,
Mar 6, 2014, 4:57:54 PM3/6/14
to nod...@googlegroups.com
Below...


fs.readFile is async, so you need to send along a callback to handle the results of the read: 


var fs = require('fs');
var sql_file = function(value, callback) {
  fs.readFile('./SQLs/inventory.sql', 'utf8', function(err, data) {
    if (err) {
      return console.log(err);
    };
    callback(data.toString());
  });
};
module.exports.sql_file = sql_file;



Rick

Bob Spero

unread,
Mar 6, 2014, 5:14:36 PM3/6/14
to nod...@googlegroups.com
I have tried it with sync and still I can not get it in the string but I can get it to console out, it has to be something stupid, I am not sure if I nested it properly or if I cam calling back the ring function.  

Aria Stewart

unread,
Mar 6, 2014, 5:32:35 PM3/6/14
to nod...@googlegroups.com
On Thu, Mar 06, 2014 at 02:14:36PM -0800, Bob Spero wrote:
> I have tried it with sync and still I can not get it in the string but I
> can get it to console out, it has to be something stupid, I am not sure if
> I nested it properly or if I cam calling back the ring function.

fs.readFileSync has a different set of arguments, so beware:

var value = fs.readFileSync('filename');

vs

var value;
fs.readFile('filename', function (err, v) {
// Some time later, after the caller has returned.
value = v;
});

Aria

Bob Spero

unread,
Mar 6, 2014, 5:48:47 PM3/6/14
to nod...@googlegroups.com
Thanks, it is sad I have been at this for about 4 hours and I know it is something fundamental i am missing because I just started with nodejs this week, ever with the call back I can not get it to work. if you look at the code I do var select_statement = "ss" and when the code completed ss is still returened, but I see the data from the file going to the console. 


On Thursday, March 6, 2014 4:50:59 PM UTC-5, Bob Spero wrote:

Bob Spero

unread,
Mar 6, 2014, 5:58:11 PM3/6/14
to nod...@googlegroups.com
Here is what I am talking about, look at return select_statement, I assign it the data but ss still comes back!



var sql_file = function(value) {

    var fileName = "./SQLs/inventory.sql";
    var select_statement = "ss";


    fs.exists(fileName, function(exists) {
        if (exists) {
            fs.stat(fileName, function(error, stats) {
                fs.open(fileName, "r", function(error, fd) {
                    var buffer = new Buffer(stats.size);
                    fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
                        var data = buffer.toString("utf8", 0, buffer.length);
                        var select_statement = data;
                        //console.log(sq);
                        return select_statement
                        fs.close(fd);
                    });
                });
            });
        }
    });

mscdex

unread,
Mar 6, 2014, 7:13:18 PM3/6/14
to nod...@googlegroups.com
On Thursday, March 6, 2014 5:58:11 PM UTC-5, Bob Spero wrote:
Here is what I am talking about, look at return select_statement, I assign it the data but ss still comes back!


You're trying to return a value inside a callback, which is useless. What you need to do instead is add a callback parameter to `sql_file` and call the callback with the `data` value (or `error` if that is set) using the node callback style:

function sql_file(cb) {
  var fileName = "./SQLs/inventory.sql";
  fs.readFile(fileName, function(err, buffer) {
    if (err) return cb(err);
    return cb(null, buffer.toString());
  });
}

// use the function ...
sql_file(function(err, sqlstatement) {
  if (err) throw err;
  console.log('sql statement is: ' + sqlstatement);
});

Bob Spero

unread,
Mar 6, 2014, 9:09:38 PM3/6/14
to nod...@googlegroups.com
mcdex can i still use export module to keep this a seperate js file from my main or do I have to keep this inside the main, this is what i needed really thanks for taking the time!

Ryan Schmidt

unread,
Mar 6, 2014, 9:10:28 PM3/6/14
to nod...@googlegroups.com

On Mar 6, 2014, at 20:09, Bob Spero wrote:

> mcdex can i still use export module to keep this a seperate js file from my main

Of course.


Paul Vencill

unread,
Mar 7, 2014, 7:32:02 AM3/7/14
to nod...@googlegroups.com
Bob, what might be easier / more useful to you would be to actually make all your SQL into key-value pairs and then make your query file a .json file. Eg

{
"allProducts" : "select * from products"
}

This way, any of your modules that need the queries can just require the json file, and have an object whose property values correspond to the SQL strings, eg

var queries = require('./queries.json');

db.query(queries.allProducts, callback);

Alex Kocharin

unread,
Mar 7, 2014, 8:12:01 AM3/7/14
to nod...@googlegroups.com

And have troubles with escaping quotes, with newlines, and with syntax highlighting. That's really bad advice. :)

If there are a few large queries, they should be in their own files with ".sql" extension. Quite a few programs are able to recognize them as is and do something useful.

If there are a lot of small queries, you can use YAML and format them like this:

---
# vim: syntax=sql
all-products: |
select * from products
all-users: |
select * from users
where something > anything
order by whatever
...


07.03.2014, 16:32, "Paul Vencill" <paul.v...@gmail.com>:
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages