L
L
LemanRass092017-02-01 16:48:37
Node.js
LemanRass09, 2017-02-01 16:48:37

TypeError: roulette.testFunc is not a function?

I ran into an incredible problem.
And it is incredible in that, in my opinion, there is nothing to break in it, because these are the basics, moreover, in other scripts of the same project, the same task is performed excellently, and in this one it is not.
It's about exporting a function and accessing it via require.
There is a test.js script and it lies along the path Site/routes/test.js.
The test function is defined in it like this:

function testFunc() {
    return "Hello world!";
}
exports.testFunc = testFunc;

There is also another socket.js script that lies along the path Site/lib/socket.js
Its contents (extra code is hidden):
var test = require('../routes/test.js');
console.log(test.testFunc());

At runtime, I get an error:
TypeError: test.testFunc is not a function
Well, what could be wrong here?
At the same time, I have 1 more log.js script which is located along the path Site/lib/log.js
It is connected to the socket.js script in the same way as test.js
var log = require('../lib/log. js');
The log script has a Debug method that prints a message to the console + text file.
exports.Debug = function(msg) {
    msg = GetDateTimeString() + " [DEBUG] " + msg;
    console.log(msg);
    fs.appendFile('logs.txt', msg + "\n", 'utf8', function (err) {
        if(err) throw err;
    });
}

And it is safely called from the socket.js script.
Well, damn it, there are 2 scripts, both are connected in the same way to 1. But at the same time, the methods of the first are called normally, and the methods of the second give an error?
While writing, only 2 options came to mind why this could happen. The only 2 differences between the included scripts.
1) The socket.js script is connected to the test.js script and then it turns out that test.js is connected to the socket.js script. This is not the case with the logo.
2) Maybe those scripts that are in routes cannot be connected because of the express?
UPD: The first option is false. I connected the socket to the log and it did not affect the work.
The second option seems to be correct. I created another file in another folder, moved this test function there from the test.js script and connected it to the socket. Result: Works fine. Apparently, scripts that are involved in the express cannot be connected from the routes folder. If anyone can formulate this rule with explanations - I will be glad.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-02-01
@bingo347

either ring dependencies or redefining module.exports
if you provide the full test.js code, it will be more accurate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question