On Mar 7, 2014, at 12:26, Bob Spero wrote:
> Also this is server side so I read you never use readfilesync on the server
> side.
Correct, using sync functions is not recommended because they block until they
complete. If you do this during the normal course of running your web server,
you will block other requests. However, it's ok to use sync functions during
the one-time setup your server may do when you first start it, or if you're
using node to write a non-server program, such as a command line script that
you might run for database maintenance or backup.
On Mar 7, 2014, at 12:20, Bob Spero wrote:
> But how do I get it in an object to do var reader =
> connection.reader(sql_file(inventory.sql), []);,
>
> On Friday, March 7, 2014 12:32:04 PM UTC-5, Bob Spero wrote:
> I am trying to create a utility that will read the select statements from the
> raw sql files, putting them in a string and executing the connection.reader.
> with something like var reader = connection.reader(sql_file(inventory.sql),
> []);, Is this possible?
>
> Util.js
> var fs = require('fs');
> function sql_file(sql_file, cb) {
> var fileName = "./SQLs/" + sql_file;
> fs.readFile(fileName, function(err, buffer) {
> if (err) return cb(err);
> return cb(null, buffer.toString());
> });
> }
> module.exports.sql_file = sql_file;
>
> Main.js
> var sqlUtil = require('./sqlUtil');
>
> var reader = connection.reader(sql_file(inventory.sql), []);
In short: you don't do that. You can only write a function that returns a value
if you use sync functions, and you shouldn't usually use sync functions, as
explained above. Instead, you have to write a function that accepts a callback
function which will be called with the result at a later time after the results
have been fetched asynchronously.
And your sql_file function is already written like that, as it was given to you
in this previous posting:
https://groups.google.com/d/msg/nodejs/9Rh6-sVqMQc/Y1DEjmnOJx0J
Since then, you've added the "sql_file" parameter to your sql_file function, so
the revised way to use the function (assuming the name of the sql file is in
the property inventory.sql, and you want to pass the sql statement to the
function connection.reader) is:
sql_file(inventory.sql, function(err, sqlstatement) {
if (err) throw err;
var reader = connection.reader(sqlstatement, []);
// now presumably do more things with reader...
});
--
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.