Reputation: 559
I am new to Node.js so this is probably a fundamental question and I am missing something obvious. In the code below, I am trying to set the sql_file name from foo but I keep getting an error on the file not existing because the variable is not working. If I hard code the file name in sql_util.js it works fine.
So how do I pass a parameter or any object from one js file into the function of another?
foo.js
var misc = require('./sql_get');
console.log(misc.sql_file("on.sql"));
sql_util.js
fs = require('fs');
file = 'on.sql'
function sql_file(cb) {
var fileName = "./SQLs/" + sql_file;
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
sql_file(function(err, sqlstatement) {
if (err) throw err;
console.log('sql statement is: ' + sqlstatement);
});
};
module.exports.x = x;
module.exports.sql_fil = sql_file;
Upvotes: 2
Views: 6808
Reputation: 10028
Let's go through this line by line because I see a lot of errors, both syntax and semantic.
foo.js
var misc = require('./sql_get');
console.log(misc.sql_file("on.sql"));
You defined in the function below sql_file
to be asynchronous, so it does not return a value, but takes a callback that it passes the result to.
sql_util.js
fs = require('fs');
file = 'on.sql'
You have an unused module-global variable file
. Remove this. It's going to cause confusion.
function sql_file(cb) {
var fileName = "./SQLs/" + sql_file;
sql_file
is a function. I can tell because this code lives in a function called sql_file
. Therefore, fileName will be "./SQLs/" + the .toString()
result of the function, which is the source of the function. I think perhaps you want a parameter?
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
This seems ok.
sql_file(function(err, sqlstatement) {
if (err) throw err;
console.log('sql statement is: ' + sqlstatement);
});
};
I am not sure what you are trying to do here. Why are you calling the function?
module.exports.x = x;
module.exports.sql_fil = sql_file;
Both of these lines have a problem. There is no symbol defined named x
. I am surprised this code runs without throwing an error on that line. Second, you are exporting sql_file
as sql_fil
, missing the letter e.
The Solution
Since what you asked about is parameter passing and you only have a single function, let's simplify this. You never want to use global variables. If a function needs a particular variable, just pass it as an argument.
foo.js
var misc = require('./sql_get');
misc.sql_file('on.sql', function (err, contents) {
console.log(contents);
});
sql_get.js (notice the file is renamed)
var fs = require('fs');
function sql_file(sqlFilename, cb) {
var fileName = "./SQLs/" + sqlFilename;
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
module.exports.sql_file = sql_file;
Upvotes: 3
Reputation: 6668
A few problems:
You're requiring sql_get but naming the other file sql_util
var misc = require('./sql_util');
You're exporting module.exports.sql_fil = sql_file;
(see the missing e
). You probably mean;
module.exports.sql_file = sql_file;
While calling sql_file, you are passing a string but expecting a cb in the function itself -
misc.sql_file("on.sql", function(err, fileContent) {
if(err) return console.log(err);
console.log('File content: ', fileContent);
});
function sql_file(sqlFileName, cb) {
var fileName = "./SQLs/" + sqlFileName;
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
And I don't know what you are doing with calling sql_file function in that file itself. Remove that.
Upvotes: 3