3

I have the following code in a script name contract.js ( foo, pwd and id are replaced by their value in my actual script)

var web3=require('web3');
personal.unlockAccount('id', "pwd")
var aContract = web3.eth.contract([]);
var a = aContract.new(
{
    from: web3.eth.accounts[1], 
    data: 'foo',
    gas: 3000000
}, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
})

I wanted to execute this code in the geth console so I typed

loadScript("/usr/username/contract.js")

But all I have is the following error:

err: TypeError: Cannot access member 'contract' of undefined false

I know that I didn't define an ABI but when I type the code above directly in the geth console it just works and then I can mine this contract (in local).

So is it really possible to obtain the same result with loadscript ?

1 Answer 1

1

Your problem is most likely that you redefine web3.

web3 exists in the geth console out of the box, already connected to the node. Unless you need a specific version other than the one geth is using, there's no need to add it manually. In this situation, it is probably breaking because require('web3') returns a factory for web3 objects, not web3 itself.

In short, try it without requiring web3.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.