3

I am trying to generate a hash of a file and send it to a contract. But I am getting an error

Error: new BigNumber() not a number: a8fac6cf98e6f5e9a76ae1f8064e505b(…)

Here's my contract code:

function sendHash(address student_id, string hash_value) returns(bool sufficient) 
{ 
    student_hashes[student_id] = hash_value;        
    return true; 
}

Here's my javascript code

function sendHash() {
  getAddresses();
  var meta = MetaCoin.deployed();

  var address = document.getElementById("studentId").value;
  var hash = (document.getElementById("hash").value);
  setStatus("Initiating transaction... (please wait)");

  meta.sendHash(account, hash).then(function() {
    setStatus("Transaction complete!");
  }).catch(function(e) {
    console.log(e);
    setStatus("Error sending contract; see log.");
  });

I am using datatype 'string' in the contract, but the error says not a number. Have tried different ways, but unable to fix this. Any help would be highly appreciated!

  • Hi, Can you tell me more about the declaration of the mapping variable student_hashes ? Are you using mapping(address => string) or mapping(address => uint) ? – Greg Jeanmart Nov 13 '16 at 11:00
  • Hi @gjeanmart I am using mapping (address => string) student_hashes; – NoviceCoder Nov 13 '16 at 17:10
  • You normally want to store hashes in a bytes32 as the length is fixed and dealing with variable-length strings in Solidity can be a PITA. – Edmund Edgar Dec 14 '16 at 22:48
1

I made it work. I didn't reproduce your issue "Error: new BigNumber() not a number:" So in first place, I would recommend to redeploy using

truffle migrate --reset

Then please below a working code :

Contract

pragma solidity ^0.4.4;
contract MetaCoin{
    mapping (address => string) student_hashes;
    function sendHash(address student_id, string hash_value) returns(bool sufficient)  { 
        student_hashes[student_id] = hash_value;        
        return true; 
    }
    function getHash(address student_id) constant returns(string hash)  {       
        return student_hashes[student_id]; 
    }
}

HTML

<!DOCTYPE html>
<html>
<head>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
  <link href="./app.css" rel='stylesheet' type='text/css'>
  <script src="./app.js"></script>
</head>
<body>
  <h1>Issue</h1>

  <br><label for="address">Address:</label><input type="text" id="studentId"></input>
  <h3>Last hash: <span class="black"><span id="lastHash"></span></span></h3>
  <button id="send" onclick="getHash()">get hash</button>

  <br>
  <h1>Send</h1>
  <br><label for="hash">Hash:</label><input type="text" id="hash"></input>
  <br><br><button id="send" onclick="sendHash()">Send hash</button>
  <br><br>
  <span id="status"></span>
</body>
</html>

Javascript

var accounts;
var account;

function setStatus(message) {
  var status = document.getElementById("status");
  status.innerHTML = message;
};
function getHash() {
  var c = MetaCoin.deployed();

  var address = document.getElementById("studentId").value;

  c.getHash.call(address).then(function(value) {
    var h_element = document.getElementById("lastHash");
    h_element.innerHTML = value.valueOf();
  }).catch(function(e) {
    console.log(e);
    setStatus("Error getting Hash; see log.");
  });
};

function sendHash() {
  var c = MetaCoin.deployed();

  var address = document.getElementById("studentId").value;
  var hash = (document.getElementById("hash").value);

  setStatus("Initiating transaction... (please wait)");

  c.sendHash(address, hash).then(function() {
    setStatus("Transaction complete!");
    getHash()
  }).catch(function(e) {
    console.log(e);
    setStatus("Error sending hash; see log.");
  });
};

window.onload = function() {
  web3.eth.getAccounts(function(err, accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];
  });
}

Just one remark : - When you send a transaction, don't forget to add at least {account: %sender address%}

| improve this answer | |

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.