I am simply trying to call my contracts on a private blockchain from a nodeJS app after a call from the browser but I keep getting this error in my node:
2017-03-01T21:07:34.782560+00:00 app[web.1]: TypeError: MyContract is not a constructor
2017-03-01T21:07:34.782562+00:00 app[web.1]: at /app/app.js:77:30
2017-03-01T21:07:34.782563+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-03-01T21:07:34.782563+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:131:13)
2017-03-01T21:07:34.782567+00:00 app[web.1]: at module.exports (/app/node_modules/express-stormpath/lib/middleware/authentication-required.js:25:12)
2017-03-01T21:07:34.782567+00:00 app[web.1]: at /app/node_modules/express-stormpath/lib/middleware/get-user.js:154:11
2017-03-01T21:07:34.782568+00:00 app[web.1]: at /app/node_modules/express-stormpath/lib/helpers/expand-account.js:145:5
2017-03-01T21:07:34.782568+00:00 app[web.1]: at /app/node_modules/async/dist/async.js:3694:9
2017-03-01T21:07:34.782569+00:00 app[web.1]: at /app/node_modules/async/dist/async.js:359:16
2017-03-01T21:07:34.782569+00:00 app[web.1]: at iteratorCallback (/app/node_modules/async/dist/async.js:935:13)
2017-03-01T21:07:34.782570+00:00 app[web.1]: at /app/node_modules/async/dist/async.js:843:16
Here is how i call my contract, what am I doing wrong ?
app.post('/api/getOrganisationDetails', stormpath.authenticationRequired, function(req, res){
var rawString = req.user.customData;
var stringJson = JSON.stringify(rawString);
var customData = JSON.parse(stringJson);
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(customData.ethereum_provider));
var coinbase = web3.eth.coinbase;
var balance = web3.eth.getBalance(coinbase);
var name = req.body.name;
var ABI = [{"constant":false,"inputs":[],
"name":"get",
"outputs":[{"name":"","type":"int8"}],
"type":"function"}];
// creation of contract object
var MyContract = web3.eth.contract(ABI);
var myContractInstance = new MyContract('0x90eb73c0bb07b0a5428b3d3ac2a5b7461bdd038b');
var result = myContractInstance.get(name);
console.log(result);
res.status(200);
res.end("hello world");
});
Best Answer
Instead of:
you should have
For example:
Also, a simple way to get the ABI and compiled code for the above call is as follows (where
MyContractSource
is your Solidity code as a string, and the field name underMyContractCompiled
- which hasinfo
andcode
sub-fields is the name of your contract in the Solidity source):