0

I made following contract using visual studio code

pragma solidity ^0.4.17;

contract IDSCheck {

    mapping( string => string ) PersonHash;

    function AddHash(string name, string hash) {
        PersonHash[name] = hash;

    }

    function GetHash (string name) public returns (string) {
        return PersonHash[name];
    }
}

Now I want to call this function from truffle using exec command.

I wrote a js file named idscheck.js with the following commands

var IDSCheck = artifacts.require("./IDSCheck.sol");
module.exports = function (deployer) {
  var ss;
  IDSCheck.deployed().then(function(deployed){ ss=deployed; });
  ss.GetHash.call("sal");
}

I get an output error saying

Type Error: cannot read property 'GetHash' of undefined at .....

I am trying to do this is in truffle console. Primary target is to create a script that runs through a batch file using truffle console to get output of the GetHash function of the above contract.

1

Your problem is the async behavior of promise returned by IDSCheck.deployed(). The variable ss might not be initialized if you call ss.GetHash() immediately after.

You can fix it by executing the call after you are sure it was intialized properly inside the .then() function.

var IDSCheck = artifacts.require("./IDSCheck.sol");
module.exports = function (callback) {
  var ss;
  IDSCheck.deployed().then(function(deployed){
    ss = deployed;
    // Here we are sure `ss` is initialized and it is safe to call `.GetHash()`
    ss.GetHash.call("sal");
  });
  // This code 'might' be excuted before the code inside `.then()`
  // ss.GetHash.call("sal");
}
0

Change this:

var IDSCheck = artifacts.require("./IDSCheck.sol");

To this:

var IDSCheck = artifacts.require("./IDSCheck.sol").new();
  • I changed but now new error is showing up .. TypeError: IDSCheck.deployed is not a function – Mubeen Sep 20 '18 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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