-2

I have this rather large switch case and it is a set of ethereum addresses I am trying to match to and output the name to a variable I can use in a different .js file. Currently I am just running the code in node in cli.

Since there are currently 16 unique names/addresses and more to come soon, I would like to keep this statement/function separate, in a separate file. I have tried many things to access the variable set in the case:, but no success. Below is a sample of what I am trying to do and was hoping someone will tell me it is possible because I am very new other than your basic tutorials that you see on every website and I could really use some clarity.

web3.eth.getBlock('latest').then(function(b){
    miners = b.miner;
    console.log(miners);
});


switch (miners) {
    case "0xE8DDc5c7f5139ffghfghfghf9c0104fDf5E987ACA":
        miner = "Miner Name 1";
        return miner;
        break;
    case "0x82e4e61e7f5139ff0a4157gfhgffgh7eF42294c248":
        miner = "Miner Name 2";
        return miner;
        break;
 }

Now this is only two of the blocks I have, but you get the picture. I was able to get what I wanted by wrapping everything in a function (including the web3 code to fetch current miner). After wrapping in a function and console.log() inside the scope, I was able to match the case no problem, but in the other file while trying to access that miner variable it was always undefined.

What I am hoping for is to not wrap the switch in a function and be able to call the miners switch from a different .js file and have the miner variable set to the name of the miner.

I'd appreciate any help and thank you in advanced to even some guidance. I have been trying different ways to do this all night. I am open to other suggestions on how to handle so many case statements. I was trying JSON object, 2d array, but seems having all the same keys for each one is going to be a problem.

hashguide
  • 1
  • 2
  • as for the switch case, couldn't you use a map between id and name? this way adding new entries would only require new entry in this map, i.e.: `const minerNames = { '0xE8DDc5c7f5139ffghfghfghf9c0104fDf5E987ACA': 'Miner Name 1', '0x82e4e61e7f5139ff0a4157gfhgffgh7eF42294c248': 'Miner Name 2' };` – Daniel Cisek Feb 27 '18 at 12:15
  • 1
    @Archer: it wasn't clear to me. Ambiguous at best. Could you maybe work with the OP to improve the wording here? – Sergio Tulentsev Feb 27 '18 at 12:32
  • 1
    @SergioTulentsev Yeah, I'll see if I can help with that – Reinstate Monica Cellio Feb 27 '18 at 12:33
  • 1
    Your question isn't very clear, but it sounds like it's simply a scoping issue as you've got the code working in a tidier format (which I see is your goal). Can you try and make the question more to the point, and maybe focus on the fact that you've got 2 files with a variable you need to access in both of them, rather than confuse the issue with information about miners, which isn't actually relevant to the problem? – Reinstate Monica Cellio Feb 27 '18 at 12:37
  • Actually, what Daniel Cisek has would actually work the way I want it and as of right now I ended up moving half the code from the other file and got it working the way I want. Thank you for the quick respones! – hashguide Feb 27 '18 at 12:53

1 Answers1

0

You have to export the variable to make accessible in the other module :

exports.miner = miner;

Then in the other module

let minerModule = require('./minermodule');

let miner = minerModule.miner;

Giona Granata
  • 549
  • 5
  • 11
  • This is originally the solution I was looking for, thank you very much. In the actual module file, do I need to put the `exports.miner = miner;` at the end of the file? – hashguide Mar 02 '18 at 23:50
  • Yes that is the standard way, although it is not compulsory. You can put it in the same line of declaration. – Giona Granata Mar 05 '18 at 16:00