简体   繁体   English

使用tablesasjson调用函数后如何更改变量的值?

[英]how to change the value of variable after calling the function using tablesasjson?

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);在尝试在函数范围外打印电子邮件时,它返回一个空白文本,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:测试模块.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:测试.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用函数后如何动态更改变量 - How to dynamically change variables after calling a function 在每个之前调用变量后,变量的值不会改变? - The value of the variable doesn't change after calling it in before each? 在Vanilla JavaScript中调用函数播放音频文件时,如何更改可变音频文件的值? - How to change a variable audio file value when calling a function to play the audiofile in Vanilla JavaScript? 如何使用函数将值更改为范围中的全局变量? (“控制器为”) - How to change the value to a global variable in the scope using a function? (“controller as”) 如何使用内部函数更改外部变量的值? - How to change value of outer variable using a inner function? 调用应更改的函数后,属性值不会更改 - Property value does not change after calling function that should have changed it 调用this.setState后函数变量值重置 - Function variable value resetting after calling this.setState 使用具有选择值的函数更改变量值 - Change Variable Value using a function with select values GAS:在处理程序函数中使用变量时,如何更改全局变量值并保留其更改后的值? - GAS: How to change a global variable value and preserve its changed value when using the variable in a handler function? 调用PHP变量作为函数值 - Calling PHP variable as function value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM