0

In my file functions.js i have two functions:

var email, url1

function getFile(_callback){
    email = fs.readFileSync("C:/Emails/" + items[2])
    _callback(email);    
}

function getUrl(){
    getLatestMail(function(email) {
            email.split(/\n/).forEach(function(line) {
            i++;
            if (i == 52) {
                var test = new Buffer(line, 'base64').toString(); 
                var regex = /=(.+?)"/g
                var result1 = regex.exec(test);
                url1 = result1[1].toString();
                console.log(url1);

        }

    });

getUrl()
exports.resetUrl = url1;

And i have a file test.js

var Functions = require('../pageobjects/functions.js');
var test = Functions.resetUrl;
console.log(test);

But it returns always undefined! The console.log in getUrl() shows the good value. It looks like that the export not is waiting until the function getURl is loaded. What is the best way to solve this? In this example i removed all the unnecessary code parts.

Janp95
  • 534
  • 8
  • 27
  • That is correct, `readFileSync` is asynchronous. You could either work around that, and as you haven't posted the code that calls `getFile` we would have no idea how you'd do that, or you could use the synchronous `readFile` instead. – adeneo Feb 16 '17 at 08:35
  • Maybe use `readFile` instead of `readFileSync`: http://stackoverflow.com/a/22863818/6429774 – Philip Feb 16 '17 at 08:35
  • 1
    You have to define `resetUrl` variable in `functions.js` – ponury-kostek Feb 16 '17 at 08:36
  • 1
    @adeneo isn't it the opposite? `readFileSync` is synchronous and `readFile` is asynchronous? – Soubhik Mondal Feb 16 '17 at 08:40
  • @BlazeSahlzen is correct: @adeneo https://nodejs.org/api/fs.html#fs_fs_readfilesync_file_options `Synchronous version of fs.readFile. Returns the contents of the file.` – Denis Tsoi Feb 16 '17 at 09:23
  • @Janp95 - can you verify if you are getting the file if you just "`require()`" - there maybe an incorrect path there. also, you can refer to using `path` and resolving the path to verify the file directory – Denis Tsoi Feb 16 '17 at 09:26
  • Just like @ponury-kostek mentioned, `resetUrl` variable is not defined in any way so you refer to something that is `undefined` - do not expect it to by something different then – piotrbienias Feb 16 '17 at 09:27
  • @BlazeSahlzen - of course it is, I got a little confused there and mixed them up. – adeneo Feb 16 '17 at 13:21

1 Answers1

0

I think that the export of functions.js should be exports.resetUrl = url1

EDIT

You must try another approach because getUrl method performs asynchronous operation, so before the url1 is set, you do the exports.resetUrl = url1, so it always will be undefined.

I suggest you to add some callback parameter to getUrl function that can be used in test.js file in order to get access to url1 value

function getUrl(callback){
    getLatestMail(function(email) {
        email.split(/\n/).forEach(function(line) {
        i++;
        if (i == 52) {
            var test = new Buffer(line, 'base64').toString(); 
            var regex = /=(.+?)"/g
            var result1 = regex.exec(test);
            url1 = result1[1].toString();
            console.log(url1);
            // and here use the callback
            callback(url1);
        }
    });
});

exports.resetUrl = getUrl;

Then you could do in test.js

var Functions = require('../pageobjects/functions.js');
Functions.resetUrl(function(url1){
    console.log(url1);
});

This is only one option that first came to my mind. I hope that you get the point and understand why url1 in your case was undefined all the time.

piotrbienias
  • 7,201
  • 1
  • 28
  • 33
  • Can you add `console.log(url1)` just before `exports` and tell what it prints – piotrbienias Feb 16 '17 at 10:16
  • Yes the console.log in the function getUrl returns the correct value. The console.log before the export returns undefined. – Janp95 Feb 16 '17 at 10:29
  • I updated the answer, check it out and tell if it helps you somehow – piotrbienias Feb 16 '17 at 11:01
  • No sorry it didn't work, the strange thing is that i see first the undefined console.log from test.js. And then the console.log from getUrl() with the correct value... – Janp95 Feb 16 '17 at 11:33
  • Where did you put the `callback(url1)` part in `getUrl` function? Because I do not see any reason why it should not work – piotrbienias Feb 16 '17 at 11:37
  • `function getUrl(callback){ getFile(function(email) { email.split(/\n/).forEach(function(line) { url1 = result1[1].toString(); console.log(url1); } }); }); callback(resetUrl); };` – Janp95 Feb 16 '17 at 12:24
  • Take a look at updated answer, the `callback` is used in `getUrl` method in the `if (i == 52)` statement, after computations and `console.log` – piotrbienias Feb 16 '17 at 12:26