0

I just asked this question and i thought it was resolved but it isnt.. so here it is again.. apologies for the double post..

I have a file: block.js:

class Block{
    constructor(timeStamp, lastBlockHash, thisBlockData, thisBlockHash){
        this.timeStamp = timeStamp;
        this.lastBlockHash = lastBlockHash;
        this.thisBlockData = thisBlockData; 
        this.thisBlockHash = thisBlockHash;
        }

    static genesis(){
        return new Block(Date.now(), "---", "This is the genesis block", "hash of the genesis");
    }
}

another file: blockchain.js:

const Block = require('./block');

class BlockChain{
    constructor() {
        this.chain = Block.genesis();
    }
}

module.exports = {BlockChain};

and finally a test file test.js:

const BlockChain = require("./blockchain.js");
let blockChainInstance = new BlockChain();

console.log(blockChainInstance.chain);

the output of the test.js file is showing "undefined" in place of a genesis block.. this has been a mystery for me since morning.. and i would be immensely grateful if anyone can solve this for me..

Cheers, al

2 Answers 2

2

Not sure if that's the root cause of your issue, but in blockchain.js you're basically exporting the class as

module.exports = {
    BlockChain: BlockChain
};

And so, in test.js you should import the class using

const { BlockChain } = require('./blockchain.js');
Sign up to request clarification or add additional context in comments.

1 Comment

i am not able to reproduce the error i had since morning.. i didnt edit anything but somehow i am not getting the same error.. if anyone is able to reproduce my original error please comment so we can have a chat.. :) cheers.. thanks Petr and everyone who spent some time
0

You have to export the Block class:

  module.exports = class Block { /*...*/ };

same for the BlockChain class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.