0

I am pretty new to this so please bare with my ignorance! I need to write some data to a contract on a private chain using a simple function that I defined. Here is the Solidity function:

 function createPatient(bytes32 firstName, bytes32 lastName, uint height, uint weight, bytes32 dob){
        PatientObjects newPatient = new PatientObjects(firstName, lastName, height, weight, dob);
        LogCreatedPatient(newPatient);
        patients.push(newPatient);
    }

It all works fine ifI use the Remix compiler online. I am now trying to access this function using web3. But I have no idea how I could do it. Here is what I have tried and the error message i got:

var createPatientForMedicalStaff = function(patient, MSaddress, provider, callback){
    console.log("in the eth api the value of patient is: " + JSON.stringify(patient));
    console.log("the address: " + MSaddress);
    var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider(provider));
    var getOrganisationDetailContract = web3.eth.contract(ethContracts.abiMedicalStaffObject).at(MSaddress);
    getOrganisationDetailContract.createPatient(patient, function(error, result){
        if (!error) {
            console.log("it worked!: " + result);
        } else {
            console.log(error);
        }
    });
};
exports.createPatientForMedicalStaff = createPatientForMedicalStaff;

The object patient contains a json with all the inputs that the solidity function needs. and MSaddress is the address of the Solidity contract. I am sure that the address is correct since when i paste it in the remix compiler I can indeed access the said contract. However when I try to run this with web3 I get this error:

 Error: invalid address                                                     2017-03-16T19:59:13.347330+00:00 app[web.1]:     at inputAddressFormatter (/app/node_modules/web3/lib/web3/formatters.js:271:11)                                                                                                                2017-03-16T19:59:13.347331+00:00 app[web.1]:     at inputTransactionFormatter (/app/node_modules/web3/lib/web3/formatters.js:97:20)                                                                                                             2017-03-16T19:59:13.347332+00:00 app[web.1]:     at Array.map (native)                                                  2017-03-16T19:59:13.347332+00:00 app[web.1]:     at /app/node_modules/web3/lib/web3/method.js:89:28                     2017-03-16T19:59:13.347333+00:00 app[web.1]:     at Method.formatInput (/app/node_modules/web3/lib/web3/method.js:88:32)                                                                                                                        2017-03-16T19:59:13.347334+00:00 app[web.1]:     at Method.toPayload (/app/node_modules/web3/lib/web3/method.js:114:23) 2017-03-16T19:59:13.347334+00:00 app[web.1]:     at Eth.send [as sendTransaction] (/app/node_modules/web3/lib/web3/method.js:139:30)                                                                                                            2017-03-16T19:59:13.347336+00:00 app[web.1]:     at SolidityFunction.execute (/app/node_modules/web3/lib/web3/function.js:234:37)                                                                                                               2017-03-16T19:59:13.347335+00:00 app[web.1]:     at SolidityFunction.sendTransaction (/app/node_modules/web3/lib/web3/function.js:151:15)                                                                                                       2017-03-16T19:59:13.347336+00:00 app[web.1]:     at Object.createPatientForMedicalStaff (/app/ethAPI.js:117:35)         2017-03-16T19:59:13.347337+00:00 app[web.1]:     at /app/app.js:174:12                                                  2017-03-16T19:59:13.347339+00:00 app[web.1]:     at module.exports (/app/node_modules/express-stormpath/lib/middleware/authentication-required.js:25:12)                                                                                        2017-03-16T19:59:13.347337+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)                                                                                                       2017-03-16T19:59:13.347338+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:131:13)         2017-03-16T19:59:13.347339+00:00 app[web.1]:     at /app/node_modules/express-stormpath/lib/middleware/get-user.js:154:11                                                                                                                       2017-03-16T19:59:13.347340+00:00 app[web.1]:     at /app/node_modules/express-stormpath/lib/helpers/expand-account.js:145:5       

1 Answer 1

1

I've noticed two things:

I. You have to respect the function signature function createPatient(bytes32 firstName, bytes32 lastName, uint height, uint weight, bytes32 dob) which means you need to pass each attribute instead of the whole Javascript object :

var createPatientForMedicalStaff = function(patient, MSaddress, provider, callback){
    console.log("in the eth api the value of patient is: " + JSON.stringify(patient));
    console.log("the address: " + MSaddress);

    var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider(provider));
    var getOrganisationDetailContract = web3.eth.contract(ethContracts.abiMedicalStaffObject).at(MSaddress);

    getOrganisationDetailContract.createPatient(patient.firstName, patient.lastName, patient.height, patient.weight, patient.dob, function(error, result){
        if (!error) {
            console.log("it worked!: " + result);
        } else {
            console.log(error);
        }
    });
};

exports.createPatientForMedicalStaff = createPatientForMedicalStaff;

II. Secondly Web3 send the transaction using the default account web3.eth.defaultAccount but it can happen this one isn't correctly set. To avoid that, refer to the list of available accounts controlled by your node

console.log(web3.eth.accounts);

And either assign an account as default account (first one for example)

web3.eth.defaultAccount=web3.eth.accounts[0]

Or configure the sender in the call using from: 0x*******

    getOrganisationDetailContract.createPatient(patient.firstName, patient.lastName, patient.height, patient.weight, patient.dob, {from: web3.eth.accounts[0]}, function(error, result){
    (...)

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.