2

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;
4
  • 1
    what's with the *'s around variable/property names? Commented Mar 7, 2014 at 14:00
  • suppose to be bold. thanks Commented Mar 7, 2014 at 14:07
  • This code is full of typos, undefined variable references and incorrect file names. I suggest cleaning up the code first and see if it works, then worry about parameter passing. Commented Mar 7, 2014 at 14:12
  • Like I said, I am new to this so I appreciate the feedback, thanks! Commented Mar 7, 2014 at 14:13

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Brandon, I am coming from a Visual Basic world and it is just such a learning curve, I have been reading Node.ja in Action, it is good but a bit over my head. Today I have Sams learn Node.Js. Really its the fundamentals that you point out that I need to focus on. I have only been working in js for about a week now so your feedback was great, thanks for taking the time! It there is any other good literature or exercises you can recommend I would be grateful.. Thanks again
Sure. JavaScript and node.js in particular are single-threaded and the trick to scaling concurrency is asynchronous I/O with callbacks. I suggest studying callbacks since there is probably no exposure to it in VB.
3

A few problems:

  1. You're requiring sql_get but naming the other file sql_util

    var misc = require('./sql_util');
    
  2. You're exporting module.exports.sql_fil = sql_file; (see the missing e). You probably mean;

    module.exports.sql_file = sql_file;
    
  3. 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.

1 Comment

I want to put the call back into var reader = connection.reader("SELECT * FROM ON", []) to keep the code neat so it looks like connection.reader(sql_file("on.sql")). Reading the raw sql file and use the select right from the sql file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.