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);
If you need to use this code as exported function from a module, you need something like 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);
}
})();
方法 convertUrl 是异步的,您不能随意在顶级代码中使用像await 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.