2

I want to get the customers last name but i get the following error any idea how to fix that ?

And is there a way to modify my current get function so that it returns the first name and last name at once ?

Unhandled Rejection (TypeError): _this.contarct.getLastName(...).call is not a function

React:

changeLastName(e){
e.preventDefault();
let id = e.target.dataset.somefield;
this.contarct.getLastName(id).call().then((lastName)=>{

this.setState({lastName:lastName});

}
);
}

<form data-somefield={this.id} onSubmit={this.changeLastName}>
<input type="text" name="id"/>
<input type="text"/>
</form>
<p>{this.state.lastName}</p>

solidity:

struct {
string lastName;

}
getLastName(unit256 _id)public view returns (string){
unit256 id = customersIndex[_id];
return customers[id].lastName;

}
1

Your contract to set and get the names:

contract mycontract{

    struct userData {
        string lastName;
        string name;
    }


    mapping(uint => userData) private costumers;

    function  setUserData(string _lastname, string _name, uint256 _id) public{
        costumers[_id].lastName = _lastname;
        costumers[_id].name = _name;
    }

    function  getUserData(uint256 _id) public view returns (string, string) {
        return (costumers[_id].lastName,costumers[_id].name);
    }

}

using web3 you can get the data like this:

ABI = 'the abi of the contract above';
var account = web3.eth.accounts[0]; 
var mycontract = web3.eth.contract(ABI);
var contractInstance = mycontract.at('contractaddress...')
contractInstance.setuserData('jane','Smith',1, function(err,res){
        if(err){
            console.log('transaction failed')
        } 
    })

for getting the user by Id:

contractInstance.getUserdata(1, function(err,res){
            if(err){
                console.log('transaction failed')
            } else{
                console.log(res)
            } 
        })

res will contain the name and the last name.

Hope this helps

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.