why the variable cannot be changed after calling the function?
this is my code:
const tabletojson = require('tabletojson');
var email ;
tabletojson.convertUrl(
'https://myurl
,
{ stripHtmlFromCells: true },
function(tablesAsJson) {
email = tablesAsJson[2][7][1];
var result2 = tablesAsJson;
console.log(result2);
var Firstname;
var lastname;
Firstname = tablesAsJson[0][1][1]
lastname = tablesAsJson[0][0][1]
console.log("Hello Sir: "+Firstname + " " +lastname + ". your email is : " + email)
console.log(email)// this prints the correct answer
}
);
> while trying to print email in out scope of the function
its returning a blank text with console.log("the email is " + email);
email is assigned in a callback function after table-to-JSON conversion is complete. If you try to use it just after the tabletojson.convertUrl() call it is still undefined because the mentioned callback will be executed asynchronously in a future loop cycle.
See:
https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
@vsemozhetbyt the issue is that i need to use the result of the variable (email) after calling it in another function which is in another file.js therefore i need to grap the value of email and not to call the function. if i can to use return of the function as a variable its ok as well
If I understand correctly, you can only call the file.js function from the callback if you need to use email from the result of the conversion.
However, it seems tabletojson provides a Promise API, so you can use async/await to make the code simpler.
Something like this:
'use strict';
const tabletojson = require('tabletojson');
(async function main() {
try {
const url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes';
const array = await tabletojson.convertUrl(url);
console.log(array[1][0]);
} catch (err) {
console.error(err);
}
})();
General questions like this should be posted to the nodejs/help repo instead.
@vsemozhetbyt thanks for this awesome solution. can i ask the last question before closing the issue please?
while trying to exports the main function and its result like this: module.exports.main = main;
module.exports.result = result; into another file.js like this var test3 = require('./test3')
test3.main();
console.log(test3.result)
its appearing that module.exports.main = main;
^
ReferenceError: main is not defined--- why that?
this is the new code (your edited code):
'use strict';
const tabletojson = require('tabletojson');
var result;
( async function main() {
try {
const url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes';
const array = await tabletojson.convertUrl(url);
result = array[1][0];
} catch (err) {
console.error(err);
}
})();
module.exports.main = main;
module.exports.result = result;
console.log(result)
In my code, function main is not declared as a variable, it is used as named IIFE. That is why ReferenceError: main is not defined.
If you need to use this code as exported function from a module, you need something llike this:
test-module.js:
'use strict';
const tabletojson = require('tabletojson');
async function getTableAsArray(url) {
try {
return await tabletojson.convertUrl(url);
} catch (err) {
console.error(err);
}
}
module.exports = {
getTableAsArray,
};
test.js:
'use strict';
const testModule = require('./test-module.js');
(async function main() {
try {
const array = await testModule.getTableAsArray(
'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes'
);
console.log(array[1][0]);
} catch (err) {
console.error(err);
}
})();
I'm closing this. Please move it to nodejs/help if follow-up is necessary.
Most helpful comment
General questions like this should be posted to the nodejs/help repo instead.