37433

Wait until fs.readFileSync is done

Question:

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.

Answer1:

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

<strong>EDIT</strong>

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.

Recommend

  • Solution: Run game local (file:///) Construct 2
  • How can I get records from Firebase where a certain field is empty
  • Any difference between ios file:///var/mobile/Containers and file:///private/var/mobile/Containers?
  • Playing multiple audio files using AVAudioPlayer
  • How to use bluebird map and return object
  • how to save ouput of perf (Linux tool) to a file?
  • Python: how to split and return a list from a function to avoid memory error
  • How to autoplay video in angularjs?
  • SOAP with Attachment / MIME content
  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • Convert RSA pem key String to der byte[]
  • Serve file to user over http via php
  • incomplete type 'struct' error in C
  • Efficient User-Agent Regex to find Safari in Python
  • Portable JRE on Linux - possible?
  • How to retrieve information from antrun back to maven?
  • Add dynamic data to line chart from mysql database with highcharts
  • Using a canvas object in a thread to do simple animations - Java
  • Extracting HTML between tags
  • How to install a .deb file on a jailbroken iphone programmatically?
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Using $this when not in object context
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Regex thinks I'm nesting, but I'm not
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Perl system calls when running as another user using sudo
  • Javascript convert timezone issue
  • JSON with duplicate key names losing information when parsed
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Jquery - Jquery Wysiwyg return html as a string
  • Why winpcap requires both .lib and .dll to run?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value