Reputation: 55
I'm doing the contract and wanna connect to the js and html by web3. When I try doing the function saveData in the contract save.sol, the ganache network didn't create the new block, but it should do.
why the contract.methods.saveData(date, resource, mycalculate, material)
in index.js didn't work?
the version of web3 is 1.5.1
the codes are shown in the following
index.html:
<!DOCTYPE html>
ignored
<div style="text-align: center;">
<button type="submit" class="btn btn-primary" id="button_formCarbonEmission">Save</button>
<a href="./home.html" class="btn btn-primary">Back</a>
</div>
<script>
const form = document.querySelector('form')
const submitButton = document.querySelector('#button_formCarbonEmission')
submitButton.addEventListener('click', function onSubmitButtonClicked(event){
form.classList.add('was-validated')
})
form.addEventListener('submit', function onFormSubmit(event){
if(!form.checkValidity()){
event.preventDefault()
event.stopPropagation()
}
})
</script>
</form>
</div>
</div>
<script src="../function/web3.min.js"></script>
<script src="../function/index.js"></script>
ignored
</body>
</html>
index.js:
// Initialize Web3
var web3 = new Web3('http://localhost:7545');
// Set Account
web3.eth.defaultAccount = '0x16E15b184F39c4fBE1A4B29052CE018c5748610B';
// Set Contract Abi
var ABI = [myabi];
// Set Contract Address
var contractAddress = 'myaddr'; // Add Your Contract address here!!!
// Set the Contract
var contract = new web3.eth.Contract(ABI, contractAddress);
// get the current time
var today = new Date();
var date = today.getFullYear()+'-' + (today.getMonth()+1) + '-' + today.getDate() + '-'
today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds;
//get the formCarbonEmission data
const submitBtn = document.getElementById("button_formCarbonEmission");
submitBtn.addEventListener('click', processFormData);
contract.methods.test().call().then(console.log);//work normally
function processFormData() {
const form = document.forms['formCarbonEmission'];
const material = form.elements.material.value;
const resource = form.elements.resource.value;
const mycalculate = form.elements.mycalculate.value;
//save the data
contract.methods.saveData(date, resource, mycalculate, material).call(); **//this function isn't work**
//alert
alert("saved!");
}
save.sol:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Storage{
string b = "calculation a";
struct UserInfo {
string time;
string source;
uint emission;
}
mapping (string => UserInfo) users;
function test() public view returns(string memory) {
return b;
}
function saveData(string memory input_time, string memory input_source, string memory input_mycalculate, uint input_material) public {
users[input_source].time = input_time;
if (keccak256(abi.encodePacked(input_mycalculate)) == keccak256(abi.encodePacked(b)))
users[input_source].emission = input_material * 3 / 2;//solidity isn't support integer
else
users[input_source].emission = input_material * 5 / 2;//solidity isn't support integer
}
/* function get() public view returns (string memory, uint, string memory) {
return (source, emission, time);
} */
function search(string memory input_source) public view returns (uint, string memory) {
return (users[input_source].emission, users[input_source].time);
}
}
Upvotes: 0
Views: 1348
Reputation: 33
contract.methods.saveData(date, resource, mycalculate, material)
has to be called with:
call(params)
- read/simulate operationsend(params)
- write operationAs you saw, your call()
haven't worked as you didn't provide any parameters but send(params)
did work.
Upvotes: 0
Reputation: 55
I solved it by creating the file "web3.js" and injected the file to the index.js
and added the account of savedata:
contract.methods.saveData(date, resource, mycalculate, material).send(
{from:web3.eth.defaultAccount}
)
Upvotes: 1