Skip to content

how to change the value of variable after calling the function using tablesasjson?  #25625

Description

@Alex1233456

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);

Activity

  1. vsemozhetbyt commented on Jan 21, 2019

    @vsemozhetbyt
    Contributor

    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/

  2. Alex1233456 commented on Jan 21, 2019

    @Alex1233456
    Author

    @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

  3. vsemozhetbyt commented on Jan 21, 2019

    @vsemozhetbyt
    Contributor

    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.

  4. vsemozhetbyt commented on Jan 21, 2019

    @vsemozhetbyt
    Contributor

    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);
      }
    })();
  5. mscdex commented on Jan 21, 2019

    @mscdex
    Contributor

    General questions like this should be posted to the nodejs/help repo instead.

  6. Alex1233456 commented on Jan 21, 2019

    @Alex1233456
    Author

    @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)
    
    
  7. vsemozhetbyt commented on Jan 21, 2019

    @vsemozhetbyt
    Contributor

    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);
      }
    })();
  8. bnoordhuis commented on Jan 22, 2019

    @bnoordhuis
    Member

    I'm closing this. Please move it to nodejs/help if follow-up is necessary.

  9. added
    wrong repoIssues that should be opened in another repository.
    on Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues asking questions about Node.js.wrong repoIssues that should be opened in another repository.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions